VOL-948: Implementation of ResourceManager module and corresponding changes in OpenOlt adapter

Change-Id: I7df8226ea1a13198f1be1e15838c69cd54890239
diff --git a/voltha/adapters/openolt/README.md b/voltha/adapters/openolt/README.md
new file mode 100644
index 0000000..5e174d3
--- /dev/null
+++ b/voltha/adapters/openolt/README.md
@@ -0,0 +1,65 @@
+# OpenOLT Device Adapter

+

+## Enable OpenOLT

+To preprovision and enable the OpenOLT use the below commands from the Voltha CLI. 

+```bash

+    (voltha) preprovision_olt -t openolt -H YOUR_OLT_MGMT_IP:9191

+    (voltha) enable

+```

+

+### Additional Notes

+1. The `bal_core_dist` and openolt driver should be running on the OLT device, before enabling the device from VOLTHA CLI.

+2. 9191 is the TCP port that the OpenOLT driver uses for its gRPC channel

+3. In the commands above, you can either use the loopback IP address (127.0.0.1) or substitute all its occurrences with the management IP of your OLT 

+

+## Using Resource Manager with Open OLT adapter

+Resource Manager is used to manage device PON resource pool and allocate PON resources

+from such pools. Resource Manager module currently manages assignment of ONU-ID, ALLOC-ID and GEM-PORT ID.

+The Resource Manager uses the KV store to back-up all the resource pool allocation data.

+

+The OpenOLT adapter interacts with Resource Manager module for PON resource assignments.

+The `openolt_resource_manager` module is responsible for interfacing with the Resource Manager.

+

+The Resource Manager optionally uses `olt_vendor_type` specific resource ranges to initialize the PON resource pools.

+In order to utilize this option, create an entry for `olt_vendor_type` specific PON resource ranges on the KV store.

+Please make sure to use the same KV store used by the VOLTHA core.

+

+### For example

+To specify ASFvOLT16 OLT device specific resource ranges, first create a JSON file `asfvolt16_resource_range.json` with the following entry

+```bash

+{

+    "onu_start_idx": 1,

+    "onu_end_idx": 127,

+    "alloc_id_start_idx": 1024,

+    "alloc_id_end_idx": 65534,

+    "gem_port_id_start_idx": 1024,

+    "gem_port_id_end_idx": 16383,

+    "num_of_pon_port": 16

+}

+```

+This data should be put on the KV store location `resource_manager/xgspon/resource_ranges/asfvolt16`

+

+The format of the KV store location is `resource_manager/<technology>/resource_ranges/<olt_vendor_type>` 

+

+In the below example the KV store is assumed to be Consul. However the same is applicable to be etcd or any other KV store.

+Please make sure to use the same KV store used by the VOLTHA core.

+```bash

+curl -X PUT -H "Content-Type: application/json" http://127.0.0.1:8500/v1/kv/resource_manager/xgspon/resource_ranges/asfvolt16 -d @./asfvolt16_resource_range.json 

+```

+

+The `olt_vendor_type` should be referred to during the preprovisiong step as shown below. The `olt_vendor_type` is an extra option and

+should be specified after `--`. The `-o` specifies the `olt_vendor_type`. 

+

+```bash

+ (voltha) preprovision_olt -t openolt -H 192.168.50.100:9191 -- -o asfvolt16

+```

+

+Once the OLT device is enabled, any further PON Resource assignments will happen within the PON Resource ranges defined

+in `asfvolt16_resource_range.json` and placed on the KV store.

+

+#### Additional Notes

+If a `default` resource range profile should be used with all `olt_vendor_type`s, then place such Resource Range profile

+at the below path on the KV store. 

+```bash

+resource_manager/xgspon/resource_ranges/default

+```

diff --git a/voltha/adapters/openolt/openolt_device.py b/voltha/adapters/openolt/openolt_device.py
index 2bf6047..20ea471 100644
--- a/voltha/adapters/openolt/openolt_device.py
+++ b/voltha/adapters/openolt/openolt_device.py
@@ -13,7 +13,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
-
+import sys
 import threading
 import binascii
 import grpc
@@ -22,6 +22,7 @@
 from twisted.internet import reactor
 from scapy.layers.l2 import Ether, Dot1Q
 from transitions import Machine
+from twisted.internet.defer import inlineCallbacks
 
 from voltha.protos.device_pb2 import Port, Device
 from voltha.protos.common_pb2 import OperStatus, AdminState, ConnectStatus
@@ -44,7 +45,9 @@
         DEFAULT_MGMT_VLAN
 from voltha.adapters.openolt.openolt_alarms import OpenOltAlarmMgr
 from voltha.adapters.openolt.openolt_bw import OpenOltBW
+from common.pon_resource_manager.resource_manager import PONResourceManager
 from voltha.extensions.alarms.onu.onu_discovery_alarm import OnuDiscoveryAlarm
+from voltha.adapters.openolt.openolt_resource_manager import OpenOltResourceMgr
 
 
 class OpenoltDevice(object):
@@ -96,6 +99,7 @@
         is_reconciliation = kwargs.get('reconciliation', False)
         self.device_id = device.id
         self.host_and_port = device.host_and_port
+        self.extra_args = device.extra_args
         self.log = structlog.get_logger(id=self.device_id,
                                         ip=self.host_and_port)
         self.proxy = registry('core').get_proxy('/')
@@ -180,6 +184,7 @@
         except Exception as e:
             self.log.exception('post_init failed', e=e)
 
+    @inlineCallbacks
     def do_state_connected(self, event):
         self.log.debug("do_state_connected")
 
@@ -195,10 +200,15 @@
         device.hardware_version = device_info.hardware_version
         device.firmware_version = device_info.firmware_version
 
+        self.resource_manager = OpenOltResourceMgr(self.device_id,
+                                                   self.host_and_port,
+                                                   self.extra_args,
+                                                   device_info)
         self.flow_mgr = OpenOltFlowMgr(self.log, self.stub, self.device_id,
-                                       self.logical_device_id)
+                                       self.logical_device_id,
+                                       self.resource_manager)
 
-        # TODO: use content of device_info for Resource manager (VOL-948)
+        yield self._initialize_resource_manager_resource_pools()
 
         # TODO: check for uptime and reboot if too long (VOL-1192)
 
@@ -278,7 +288,6 @@
         self.log.debug('post_down')
         self.flow_mgr.reset_flows()
 
-
     def indications_thread(self):
         self.log.debug('starting-indications-thread')
         self.log.debug('connecting to olt', device_id=self.device_id)
@@ -377,6 +386,7 @@
             # FIXME - handle PON oper state change
             pass
 
+    @inlineCallbacks
     def onu_discovery_indication(self, onu_disc_indication):
         intf_id = onu_disc_indication.intf_id
         serial_number = onu_disc_indication.serial_number
@@ -400,14 +410,32 @@
             serial_number=serial_number_str)
 
         if onu_device is None:
-            onu_id = self.new_onu_id(intf_id)
             try:
+                onu_id = yield self.resource_manager.get_resource_id(
+                    intf_id, PONResourceManager.ONU_ID)
+                if onu_id is None:
+                    raise Exception("onu-id-unavailable")
+
+                pon_intf_onu_id = (intf_id, onu_id)
+                self.resource_manager.init_resource_store(pon_intf_onu_id)
+
+                alloc_id = yield self.resource_manager.get_alloc_id(
+                    pon_intf_onu_id)
+                if alloc_id is None:
+                    # Free up other PON resources if are unable to
+                    # proceed ahead
+                    self.resource_manager.free_resource_id(
+                        intf_id, PONResourceManager.ONU_ID, onu_id
+                    )
+                    raise Exception("alloc-id-unavailable")
+
                 self.add_onu_device(
                     intf_id,
                     platform.intf_id_to_port_no(intf_id, Port.PON_OLT),
                     onu_id, serial_number)
+                # Use sched_id same as alloc_id for the ONU.
                 self.activate_onu(intf_id, onu_id, serial_number,
-                                  serial_number_str)
+                                  serial_number_str, alloc_id, alloc_id)
             except Exception as e:
                 self.log.exception('onu-activation-failed', e=e)
 
@@ -436,8 +464,12 @@
                 onu_device.oper_status = OperStatus.DISCOVERED
                 self.adapter_agent.update_device(onu_device)
                 try:
+                    pon_intf_onu_id = (intf_id, onu_id)
+                    alloc_id = yield self.resource_manager.get_alloc_id(
+                        pon_intf_onu_id)
+                    # Use sched_id same as alloc_id for the ONU.
                     self.activate_onu(intf_id, onu_id, serial_number,
-                                      serial_number_str)
+                                      serial_number_str, alloc_id, alloc_id)
                 except Exception as e:
                     self.log.error('onu-activation-error',
                                    serial_number=serial_number_str, error=e)
@@ -445,6 +477,7 @@
                 self.log.warn('unexpected state', onu_id=onu_id,
                               onu_device_oper_state=onu_device.oper_status)
 
+    @inlineCallbacks
     def onu_indication(self, onu_indication):
         self.log.debug("onu indication", intf_id=onu_indication.intf_id,
                        onu_id=onu_indication.onu_id,
@@ -473,6 +506,12 @@
                            onu_id=onu_indication.onu_id)
             return
 
+
+        # We will use this alloc_id and gemport_id to pass on to the onu adapter
+        pon_intf_onu_id = (onu_indication.intf_id, onu_indication.onu_id)
+        alloc_id = yield self.resource_manager.get_alloc_id(pon_intf_onu_id)
+        gemport_id = yield self.resource_manager.get_gemport_id(pon_intf_onu_id)
+
         if platform.intf_id_from_pon_port_no(onu_device.parent_port_no) \
                 != onu_indication.intf_id:
             self.log.warn('ONU-is-on-a-different-intf-id-now',
@@ -574,14 +613,11 @@
 
                 # tcont creation (onu)
                 tcont = TcontsConfigData()
-                tcont.alloc_id = platform.mk_alloc_id(
-                    onu_indication.intf_id, onu_indication.onu_id)
+                tcont.alloc_id = alloc_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.gemport_id = gemport_id
 
                 # ports creation/update
                 def port_config():
@@ -625,14 +661,12 @@
 
                 # tcont creation (onu)
                 tcont = TcontsConfigData()
-                tcont.alloc_id = platform.mk_alloc_id(
-                    onu_indication.intf_id, onu_indication.onu_id)
+                tcont.alloc_id = alloc_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.gemport_id = gemport_id
+
                 gem_port.tcont_ref = str(tcont.alloc_id)
 
                 self.log.info('inject-tcont-gem-data-onu-handler',
@@ -696,7 +730,7 @@
         onu_device = self.adapter_agent.get_child_device(
             self.device_id, onu_id=omci_indication.onu_id,
             parent_port_no=platform.intf_id_to_port_no(
-                            omci_indication.intf_id, Port.PON_OLT),)
+                omci_indication.intf_id, Port.PON_OLT), )
 
         self.adapter_agent.receive_proxied_message(onu_device.proxy_address,
                                                    omci_indication.pkt)
@@ -706,8 +740,17 @@
         self.log.debug("packet indication", intf_id=pkt_indication.intf_id,
                        gemport_id=pkt_indication.gemport_id,
                        flow_id=pkt_indication.flow_id)
+        onu_id = None
 
-        onu_id = platform.onu_id_from_gemport_id(pkt_indication.gemport_id)
+        pon_intf_gemport = (pkt_indication.intf_id, pkt_indication.gemport_id)
+        try:
+            onu_id = self.resource_manager.pon_intf_gemport_to_onu_id_map[
+                pon_intf_gemport]
+        except KeyError:
+            self.log.error("no-onu-reference-for-gem",
+                           gemport_id=pkt_indication.gemport_id)
+            return
+
         logical_port_num = platform.mk_uni_port_num(pkt_indication.intf_id,
                                                     onu_id)
 
@@ -773,10 +816,10 @@
 
     def send_proxied_message(self, proxy_address, msg):
         onu_device = self.adapter_agent.get_child_device(
-            self.device_id,
-            onu_id=proxy_address.onu_id,
-            parent_port_no=platform.intf_id_to_port_no(
-                proxy_address.channel_id, Port.PON_OLT))
+            self.device_id, onu_id=proxy_address.onu_id,
+                parent_port_no=platform.intf_id_to_port_no(
+                proxy_address.channel_id, Port.PON_OLT)
+        )
         if onu_device.connect_status != ConnectStatus.REACHABLE:
             self.log.debug('ONU is not reachable, cannot send OMCI',
                            serial_number=onu_device.serial_number,
@@ -900,19 +943,6 @@
                 self.adapter_agent.delete_port(self.device_id, port)
                 return
 
-    def new_onu_id(self, intf_id):
-        onu_devices = self.adapter_agent.get_child_devices(self.device_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 update_flow_table(self, flows):
         self.log.debug('No updates here now, all is done in logical flows '
                        'update')
@@ -1005,6 +1035,10 @@
         self.log.info('deleting-olt', device_id=self.device_id,
                       logical_device_id=self.logical_device_id)
 
+        # Removes all data from the resource manager KV store
+        # for the device.
+        self.resource_manager.clear_device_resource_pool()
+
         try:
             # Rebooting to reset the state
             self.reboot()
@@ -1031,16 +1065,18 @@
             self.log.info('openolt device reenabled')
 
     def activate_onu(self, intf_id, onu_id, serial_number,
-                     serial_number_str):
+                     serial_number_str, agg_port_id, sched_id):
         pir = self.bw_mgr.pir(serial_number_str)
         self.log.debug("activating-onu", intf_id=intf_id, onu_id=onu_id,
                        serial_number_str=serial_number_str,
                        serial_number=serial_number, pir=pir)
         onu = openolt_pb2.Onu(intf_id=intf_id, onu_id=onu_id,
-                              serial_number=serial_number, pir=pir)
+                              serial_number=serial_number, pir=pir,
+                              agg_port_id=agg_port_id, sched_id=sched_id)
         self.stub.ActivateOnu(onu)
         self.log.info('onu-activated', serial_number=serial_number_str)
 
+    @inlineCallbacks
     def delete_child_device(self, child_device):
         self.log.debug('sending-deactivate-onu',
                        olt_device_id=self.device_id,
@@ -1062,11 +1098,25 @@
             self.log.error('port delete error', error=e)
         serial_number = self.destringify_serial_number(
             child_device.serial_number)
+        pon_intf_id_onu_id = (child_device.proxy_address.channel_id,
+                              child_device.proxy_address.onu_id)
+        alloc_id = yield self.resource_manager.get_alloc_id(pon_intf_id_onu_id)
+        # Use sched_id same as alloc_id for the Onu.
         onu = openolt_pb2.Onu(intf_id=child_device.proxy_address.channel_id,
                               onu_id=child_device.proxy_address.onu_id,
-                              serial_number=serial_number)
+                              serial_number=serial_number,
+                              agg_port_id=alloc_id, sched_id=alloc_id)
         self.stub.DeleteOnu(onu)
 
+        # Remove reference to (pon_intf_id, onu_id) from the dictionary
+        # when ONU is being removed.
+        del self.resource_manager.pon_intf_id_onu_id_to_resource_map[
+            pon_intf_id_onu_id]
+        # Free any PON resources that were reserved for the ONU
+        self.resource_manager.free_pon_resources_for_onu(
+            child_device.proxy_address.channel_id,
+                                         child_device.proxy_address.onu_id)
+
     def reboot(self):
         self.log.debug('rebooting openolt device', device_id=self.device_id)
         try:
@@ -1087,3 +1137,14 @@
 
     def simulate_alarm(self, alarm):
         self.alarm_mgr.simulate_alarm(alarm)
+
+    @inlineCallbacks
+    def _initialize_resource_manager_resource_pools(self):
+        try:
+            yield self.resource_manager.initialize_device_resource_range_and_pool()
+        except Exception as e:
+            # When resource manager initialization fails we can't proceed
+            # further so, exiting without instantiating openolt adapter
+            self.log.exception("Resource-manager-initialization-failed", e=e)
+            sys.exit(1)
+
diff --git a/voltha/adapters/openolt/openolt_flow_mgr.py b/voltha/adapters/openolt/openolt_flow_mgr.py
index 4c40910..692f639 100644
--- a/voltha/adapters/openolt/openolt_flow_mgr.py
+++ b/voltha/adapters/openolt/openolt_flow_mgr.py
@@ -16,6 +16,7 @@
 import copy
 from twisted.internet import reactor
 import grpc
+from twisted.internet.defer import inlineCallbacks, returnValue
 
 from voltha.protos.openflow_13_pb2 import OFPXMC_OPENFLOW_BASIC, \
     ofp_flow_stats, OFPMT_OXM, Flows, FlowGroups, OFPXMT_OFB_IN_PORT, \
@@ -23,6 +24,7 @@
 from voltha.protos.device_pb2 import Port
 import voltha.core.flow_decomposer as fd
 import openolt_platform as platform
+from common.pon_resource_manager.resource_manager import  PONResourceManager
 from voltha.adapters.openolt.protos import openolt_pb2
 from voltha.registry import registry
 
@@ -44,7 +46,7 @@
 
 class OpenOltFlowMgr(object):
 
-    def __init__(self, log, stub, device_id, logical_device_id):
+    def __init__(self, log, stub, device_id, logical_device_id, resource_mgr):
         self.log = log
         self.stub = stub
         self.device_id = device_id
@@ -54,6 +56,7 @@
         self.flows_proxy = registry('core').get_proxy(
             '/devices/{}/flows'.format(self.device_id))
         self.root_proxy = registry('core').get_proxy('/')
+        self.resource_mgr = resource_mgr
 
     def add_flow(self, flow):
         self.log.debug('add flow', flow=flow)
@@ -209,6 +212,7 @@
             self.log.debug('no device flow to remove for this flow (normal '
                            'for multi table flows)', flow=flow)
 
+    @inlineCallbacks
     def divide_and_add_flow(self, intf_id, onu_id, classifier,
                             action, flow):
 
@@ -218,7 +222,7 @@
         if 'ip_proto' in classifier:
             if classifier['ip_proto'] == 17:
                 self.log.debug('dhcp flow add')
-                self.add_dhcp_trap(intf_id, onu_id, classifier,
+                yield self.add_dhcp_trap(intf_id, onu_id, classifier,
                                    action, flow)
             elif classifier['ip_proto'] == 2:
                 self.log.warn('igmp flow add ignored, not implemented yet')
@@ -229,48 +233,49 @@
         elif 'eth_type' in classifier:
             if classifier['eth_type'] == EAP_ETH_TYPE:
                 self.log.debug('eapol flow add')
-                self.add_eapol_flow(intf_id, onu_id, flow)
+                yield self.add_eapol_flow(intf_id, onu_id, flow)
                 vlan_id = self.get_subscriber_vlan(fd.get_in_port(flow))
                 if vlan_id is not None:
-                    self.add_eapol_flow(
+                    yield self.add_eapol_flow(
                         intf_id, onu_id, flow,
                         uplink_eapol_id=EAPOL_UPLINK_SECONDARY_FLOW_INDEX,
                         downlink_eapol_id=EAPOL_DOWNLINK_SECONDARY_FLOW_INDEX,
                         vlan_id=vlan_id)
             if classifier['eth_type'] == LLDP_ETH_TYPE:
                 self.log.debug('lldp flow add')
-                self.add_lldp_flow(intf_id, onu_id, flow, classifier,
+                yield self.add_lldp_flow(intf_id, onu_id, flow, classifier,
                                    action)
 
         elif 'push_vlan' in action:
-            self.add_upstream_data_flow(intf_id, onu_id, classifier, action,
-                                        flow)
+            yield self.add_upstream_data_flow(intf_id, onu_id, classifier, action,
+                                      flow)
         elif 'pop_vlan' in action:
-            self.add_downstream_data_flow(intf_id, onu_id, classifier,
+            yield self.add_downstream_data_flow(intf_id, onu_id, classifier,
                                           action, flow)
         else:
             self.log.debug('Invalid-flow-type-to-handle',
                            classifier=classifier,
                            action=action, flow=flow)
 
+    @inlineCallbacks
     def add_upstream_data_flow(self, intf_id, onu_id, uplink_classifier,
                                uplink_action, logical_flow):
 
         uplink_classifier['pkt_tag_type'] = 'single_tag'
 
-        self.add_hsia_flow(intf_id, onu_id, uplink_classifier,
+        yield self.add_hsia_flow(intf_id, onu_id, uplink_classifier,
                            uplink_action, 'upstream', HSIA_FLOW_INDEX,
                            logical_flow)
 
         # Secondary EAP on the subscriber vlan
         (eap_active, eap_logical_flow) = self.is_eap_enabled(intf_id, onu_id)
         if eap_active:
-            self.add_eapol_flow(
-                intf_id, onu_id, eap_logical_flow,
+            yield self.add_eapol_flow(intf_id, onu_id, eap_logical_flow,
                 uplink_eapol_id=EAPOL_UPLINK_SECONDARY_FLOW_INDEX,
                 downlink_eapol_id=EAPOL_DOWNLINK_SECONDARY_FLOW_INDEX,
                 vlan_id=uplink_classifier['vlan_vid'])
 
+    @inlineCallbacks
     def add_downstream_data_flow(self, intf_id, onu_id, downlink_classifier,
                                  downlink_action, flow):
         downlink_classifier['pkt_tag_type'] = 'double_tag'
@@ -278,7 +283,7 @@
         downlink_action['pop_vlan'] = True
         downlink_action['vlan_vid'] = downlink_classifier['vlan_vid']
 
-        self.add_hsia_flow(intf_id, onu_id, downlink_classifier,
+        yield self.add_hsia_flow(intf_id, onu_id, downlink_classifier,
                            downlink_action, 'downstream', HSIA_FLOW_INDEX,
                            flow)
 
@@ -286,21 +291,31 @@
     # will take care of handling all the p bits.
     # We need to revisit when mulitple gem port per p bits is needed.
     # Waiting for Technology profile
+    @inlineCallbacks
     def add_hsia_flow(self, intf_id, onu_id, classifier, action,
                       direction, hsia_id, logical_flow):
 
-        gemport_id = platform.mk_gemport_id(intf_id, onu_id)
+        pon_intf_onu_id = (intf_id, onu_id)
+        gemport_id = yield self.resource_mgr.get_gemport_id(
+                          pon_intf_onu_id=pon_intf_onu_id
+                     )
+        alloc_id = yield self.resource_mgr.get_alloc_id(
+                          pon_intf_onu_id=pon_intf_onu_id
+                     )
+
         flow_id = platform.mk_flow_id(intf_id, onu_id, hsia_id)
 
         flow = openolt_pb2.Flow(
                 onu_id=onu_id, flow_id=flow_id, flow_type=direction,
                 access_intf_id=intf_id, gemport_id=gemport_id,
+                alloc_id=alloc_id,
                 priority=logical_flow.priority,
                 classifier=self.mk_classifier(classifier),
                 action=self.mk_action(action))
 
         self.add_flow_to_device(flow, logical_flow)
 
+    @inlineCallbacks
     def add_dhcp_trap(self, intf_id, onu_id, classifier, action, logical_flow):
 
         self.log.debug('add dhcp upstream trap', classifier=classifier,
@@ -311,12 +326,19 @@
         classifier['pkt_tag_type'] = 'single_tag'
         classifier.pop('vlan_vid', None)
 
-        gemport_id = platform.mk_gemport_id(intf_id, onu_id)
+        pon_intf_onu_id = (intf_id, onu_id)
+        gemport_id = yield self.resource_mgr.get_gemport_id(
+                          pon_intf_onu_id=pon_intf_onu_id
+                     )
+        alloc_id = yield self.resource_mgr.get_alloc_id(
+                          pon_intf_onu_id=pon_intf_onu_id
+                     )
         flow_id = platform.mk_flow_id(intf_id, onu_id, DHCP_FLOW_INDEX)
 
         upstream_flow = openolt_pb2.Flow(
             onu_id=onu_id, flow_id=flow_id, flow_type="upstream",
             access_intf_id=intf_id, gemport_id=gemport_id,
+            alloc_id=alloc_id,
             priority=logical_flow.priority,
             classifier=self.mk_classifier(classifier),
             action=self.mk_action(action))
@@ -349,6 +371,7 @@
 
         self.add_flow_to_device(downstream_flow, downstream_logical_flow)
 
+    @inlineCallbacks
     def add_eapol_flow(self, intf_id, onu_id, logical_flow,
                        uplink_eapol_id=EAPOL_FLOW_INDEX,
                        downlink_eapol_id=EAPOL_DOWNLINK_FLOW_INDEX,
@@ -364,7 +387,13 @@
         uplink_action = {}
         uplink_action['trap_to_host'] = True
 
-        gemport_id = platform.mk_gemport_id(intf_id, onu_id)
+        pon_intf_onu_id = (intf_id, onu_id)
+        gemport_id = yield self.resource_mgr.get_gemport_id(
+                          pon_intf_onu_id=pon_intf_onu_id
+                     )
+        alloc_id = yield self.resource_mgr.get_alloc_id(
+                          pon_intf_onu_id=pon_intf_onu_id
+                     )
 
         # Add Upstream EAPOL Flow.
 
@@ -373,6 +402,7 @@
         upstream_flow = openolt_pb2.Flow(
             onu_id=onu_id, flow_id=uplink_flow_id, flow_type="upstream",
             access_intf_id=intf_id, gemport_id=gemport_id,
+            alloc_id=alloc_id,
             priority=logical_flow.priority,
             classifier=self.mk_classifier(uplink_classifier),
             action=self.mk_action(uplink_action))
@@ -439,6 +469,7 @@
     def reset_flows(self):
         self.flows_proxy.update('/', Flows())
 
+    @inlineCallbacks
     def add_lldp_flow(self, intf_id, onu_id, logical_flow, classifier, action):
 
         self.log.debug('add lldp downstream trap', classifier=classifier,
@@ -448,12 +479,20 @@
         action['trap_to_host'] = True
         classifier['pkt_tag_type'] = 'untagged'
 
-        gemport_id = platform.mk_gemport_id(onu_id)
+        pon_intf_onu_id = (intf_id, onu_id)
+        gemport_id = yield self.resource_mgr.get_gemport_id(
+                          pon_intf_onu_id=pon_intf_onu_id
+                     )
+        alloc_id = yield self.resource_mgr.get_alloc_id(
+                          pon_intf_onu_id=pon_intf_onu_id
+                     )
+
         flow_id = platform.mk_flow_id(intf_id, onu_id, LLDP_FLOW_INDEX)
 
         downstream_flow = openolt_pb2.Flow(
             onu_id=onu_id, flow_id=flow_id, flow_type="downstream",
             access_intf_id=3, network_intf_id=0, gemport_id=gemport_id,
+            alloc_id=alloc_id,
             priority=logical_flow.priority,
             classifier=self.mk_classifier(classifier),
             action=self.mk_action(action))
diff --git a/voltha/adapters/openolt/openolt_platform.py b/voltha/adapters/openolt/openolt_platform.py
index a7f6904..b179d90 100644
--- a/voltha/adapters/openolt/openolt_platform.py
+++ b/voltha/adapters/openolt/openolt_platform.py
@@ -21,34 +21,6 @@
 Encoding of identifiers
 =======================
 
-GEM port ID
-
-    GEM port id is unique per PON port
-
-     10              3      0
-    +--+--------------+------+
-    |1 |     onu id   | GEM  |
-    |  |              | idx  |
-    +--+--------------+------+
-
-    GEM port id range (0, 1023) is reserved
-    onu id = 7 bits = 128 ONUs per PON
-    GEM index = 3 bits = 8 GEM ports per ONU
-
-Alloc ID
-
-    Uniquely identifies a T-CONT
-    Ranges from 0 to 4095
-    Unique per PON interface
-
-     12         6            0
-    +------------+------------+
-    |   onu id   | Alloc idx  |
-    +------------+------------+
-
-    onu id = 7 bits = 128 ONUs per PON
-    Alloc index = 6 bits = 64 GEM ports per ONU
-
 Flow id
 
     Identifies a flow within a single OLT
@@ -109,20 +81,6 @@
 def mk_uni_port_num(intf_id, onu_id):
     return intf_id << 11 | onu_id << 4
 
-
-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 + intf_id * MAX_ONUS_PER_PON + onu_id  # FIXME
-
-
-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 - 1024) // 7) % MAX_ONUS_PER_PON) + 1
-
-
 def mk_flow_id(intf_id, onu_id, idx):
     return intf_id << 11 | onu_id << 4 | idx
 
@@ -184,3 +142,4 @@
         return True
 
     return False
+
diff --git a/voltha/adapters/openolt/openolt_resource_manager.py b/voltha/adapters/openolt/openolt_resource_manager.py
new file mode 100644
index 0000000..a85cbc5
--- /dev/null
+++ b/voltha/adapters/openolt/openolt_resource_manager.py
@@ -0,0 +1,276 @@
+#

+# Copyright 2018 the original author or authors.

+#

+# 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.

+#

+import shlex

+from argparse import ArgumentParser, ArgumentError

+

+import structlog

+from twisted.internet.defer import inlineCallbacks, returnValue

+

+from common.pon_resource_manager.resource_manager import PONResourceManager

+from voltha.registry import registry

+

+

+# Used to parse extra arguments to OpenOlt adapter from the NBI

+class OpenOltArgumentParser(ArgumentParser):

+    # Must override the exit command to prevent it from

+    # calling sys.exit().  Return exception instead.

+    def exit(self, status=0, message=None):

+        raise Exception(message)

+

+

+class OpenOltResourceMgr(object):

+

+    GEMPORT_IDS = "gemport_ids"

+    ALLOC_IDS = "alloc_ids"

+

+    def __init__(self, device_id, host_and_port, extra_args, device_info):

+        self.log = structlog.get_logger(id=device_id,

+                                        ip=host_and_port)

+        self.device_id = device_id

+        self.host_and_port = host_and_port

+        self.extra_args = extra_args

+        self.device_info = device_info

+        # Below dictionary maintains a map of tuple (pon_intf_id, onu_id)

+        # to list of gemports and alloc_ids.

+        # Note: This is a stateful information and will be lost across

+        # VOLTHA reboots. When adapter is containerized, this information

+        # should be backed up in an external K/V store.

+        '''

+        Example:

+        {

+            (pon_intf_id_1, onu_id_1): {

+                    "gemport_ids": [1024,1025],

+                    "alloc_ids"  : [1024]

+            },

+            (pon_intf_id_1, onu_id_2): {

+                    "gemport_ids": [1026],

+                    "alloc_ids"  : [1025]

+            }

+        }

+        '''

+        self.pon_intf_id_onu_id_to_resource_map = dict()

+        self.pon_intf_gemport_to_onu_id_map = dict()

+        self.resource_mgr = self.\

+            _parse_extra_args_and_init_resource_manager_class()

+        # Flag to indicate whether information fetched from device should

+        # be used to intialize PON Resource Ranges

+        self.use_device_info = False

+

+    def _parse_extra_args_and_init_resource_manager_class(self):

+        self.args = registry('main').get_args()

+

+        # KV store's IP Address and PORT

+        host, port = '127.0.0.1', 8500

+        if self.args.backend == 'etcd':

+            host, port = self.args.etcd.split(':', 1)

+        elif self.args.backend == 'consul':

+            host, port = self.args.consul.split(':', 1)

+

+        if self.extra_args and len(self.extra_args) > 0:

+            parser = OpenOltArgumentParser(add_help=False)

+            parser.add_argument('--openolt_variant', '-o', action='store',

+                                choices=['default', 'asfvolt16'],

+                                default='default')

+            try:

+                args = parser.parse_args(shlex.split(self.extra_args))

+                self.log.debug('parsing-extra-arguments', args=args)

+

+                try:

+                    resource_manager = PONResourceManager(

+                        self.device_info.technology,

+                        args.openolt_variant,

+                        self.device_id, self.args.backend,

+                        host, port

+                    )

+                except Exception as e:

+                    raise Exception(e)

+

+            except ArgumentError as e:

+                raise Exception('invalid-arguments: {}'.format(e.message))

+

+            except Exception as e:

+                raise Exception(

+                    'option-parsing-error: {}'.format(e.message))

+        else:

+            try:

+                # OLT Vendor type not available, use device information

+                # to initialize PON resource ranges.

+                self.use_device_info = True

+

+                resource_manager = PONResourceManager(

+                    self.device_info.technology,

+                    self.device_info.vendor,

+                    self.device_id, self.args.backend,

+                    host, port

+                )

+            except Exception as e:

+                raise Exception(e)

+

+        return resource_manager

+

+    def init_resource_store(self, pon_intf_onu_id):

+        # Initialize the map to store the (pon_intf_id, onu_id) to gemport

+        # list and alloc_id list

+        if pon_intf_onu_id not in \

+                self.pon_intf_id_onu_id_to_resource_map.keys():

+            self.pon_intf_id_onu_id_to_resource_map[pon_intf_onu_id] = dict()

+            self.pon_intf_id_onu_id_to_resource_map[pon_intf_onu_id] \

+                [OpenOltResourceMgr.GEMPORT_IDS] = list()

+            self.pon_intf_id_onu_id_to_resource_map[pon_intf_onu_id] \

+                [OpenOltResourceMgr.ALLOC_IDS] = list()

+

+    @inlineCallbacks

+    def get_resource_id(self, pon_intf_id, resource_type, num_of_id=1):

+        resource = yield self.resource_mgr.get_resource_id(

+            pon_intf_id, resource_type, num_of_id)

+        returnValue(resource)

+

+    @inlineCallbacks

+    def free_resource_id(self, pon_intf_id, resource_type, release_content):

+        result = yield self.resource_mgr.free_resource_id(

+            pon_intf_id, resource_type, release_content)

+        returnValue(result)

+

+    @inlineCallbacks

+    def get_alloc_id(self, pon_intf_onu_id):

+        # Derive the pon_intf from the pon_intf_onu_id tuple

+        pon_intf = pon_intf_onu_id[0]

+        alloc_id = None

+

+        # Since we support only one alloc_id for the ONU at the moment,

+        # return the first alloc_id in the list, if available, for that

+        # ONU.

+        if len(self.pon_intf_id_onu_id_to_resource_map[pon_intf_onu_id] \

+                       [OpenOltResourceMgr.ALLOC_IDS]) > 0:

+            alloc_id = self.pon_intf_id_onu_id_to_resource_map[

+                pon_intf_onu_id][OpenOltResourceMgr.ALLOC_IDS][0]

+            returnValue(alloc_id)

+

+        # get_alloc_id returns a list of alloc_id.

+        alloc_id_list = yield self.resource_mgr.get_resource_id(

+            pon_intf_id=pon_intf,

+            resource_type=PONResourceManager.ALLOC_ID,

+            num_of_id=1

+        )

+        if alloc_id_list and len(alloc_id_list) == 0:

+            self.log.error("no-alloc-id-available")

+            returnValue(alloc_id)

+

+        # store the alloc id list per (pon_intf_id, onu_id) tuple

+        self.pon_intf_id_onu_id_to_resource_map[pon_intf_onu_id] \

+            [OpenOltResourceMgr.ALLOC_IDS].extend(alloc_id_list)

+

+        # Since we request only one alloc id, we refer the 0th

+        # index

+        alloc_id = alloc_id_list[0]

+

+        returnValue(alloc_id)

+

+    @inlineCallbacks

+    def get_gemport_id(self, pon_intf_onu_id):

+        # Derive the pon_intf and onu_id from the pon_intf_onu_id tuple

+        pon_intf = pon_intf_onu_id[0]

+        onu_id = pon_intf_onu_id[1]

+        gemport = None

+

+        if len(self.pon_intf_id_onu_id_to_resource_map[pon_intf_onu_id] \

+                       [OpenOltResourceMgr.GEMPORT_IDS]) > 0:

+            # Since we support only one gemport_id on the ONU at the moment,

+            # return the first gemport_id in the list, if available, for that

+            # ONU.

+            gemport = self.pon_intf_id_onu_id_to_resource_map[pon_intf_onu_id] \

+                [OpenOltResourceMgr.GEMPORT_IDS][0]

+            returnValue(gemport)

+

+        # get_gem_id returns a list of gem_id.

+        gemport_id_list = yield self.resource_mgr.get_resource_id(

+            pon_intf_id=pon_intf,

+            resource_type=PONResourceManager.GEMPORT_ID,

+            num_of_id=1

+        )

+

+        if gemport_id_list and len(gemport_id_list) == 0:

+            self.log.error("no-gemport-id-available")

+            returnValue(gemport)

+

+        # store the gem port id list per (pon_intf_id, onu_id) tuple

+        self.pon_intf_id_onu_id_to_resource_map[pon_intf_onu_id] \

+            [OpenOltResourceMgr.GEMPORT_IDS].extend(gemport_id_list)

+

+        # We currently use only one gemport

+        gemport = gemport_id_list[0]

+

+        pon_intf_gemport = (pon_intf, gemport)

+        # This information is used when packet_indication is received and

+        # we need to derive the ONU Id for which the packet arrived based

+        # on the pon_intf and gemport available in the packet_indication

+        self.pon_intf_gemport_to_onu_id_map[pon_intf_gemport] = onu_id

+

+        returnValue(gemport)

+

+    def free_pon_resources_for_onu(self, pon_intf_id, onu_id):

+        # Frees Alloc Ids and Gemport Ids from Resource Manager for

+        # a given onu on a particular pon port

+

+        pon_intf_id_onu_id = (pon_intf_id, onu_id)

+        alloc_ids = \

+            self.pon_intf_id_onu_id_to_resource_map[pon_intf_id_onu_id] \

+                [OpenOltResourceMgr.ALLOC_IDS]

+        gemport_ids = \

+            self.pon_intf_id_onu_id_to_resource_map[pon_intf_id_onu_id] \

+                [OpenOltResourceMgr.GEMPORT_IDS]

+        self.resource_mgr.free_resource_id(pon_intf_id,

+                                               PONResourceManager.ONU_ID,

+                                               onu_id)

+        self.resource_mgr.free_resource_id(pon_intf_id,

+                                               PONResourceManager.ALLOC_ID,

+                                               alloc_ids)

+        self.resource_mgr.free_resource_id(pon_intf_id,

+                                               PONResourceManager.GEMPORT_ID,

+                                               gemport_ids)

+

+        # We need to clear the mapping of (pon_intf_id, gemport_id) to onu_id

+        for gemport_id in gemport_ids:

+            del self.pon_intf_gemport_to_onu_id_map[(pon_intf_id, gemport_id)]

+

+    @inlineCallbacks

+    def initialize_device_resource_range_and_pool(self):

+        if not self.use_device_info:

+            status = yield self.resource_mgr.init_resource_ranges_from_kv_store()

+            if not status:

+                self.log.error("failed-to-load-resource-range-from-kv-store")

+                # When we have failed to read the PON Resource ranges from KV

+                # store, use the information fetched from device.

+                self.use_device_info = True

+

+        if self.use_device_info:

+            self.log.info("using-device-info-to-init-pon-resource-ranges")

+            self.resource_mgr.init_default_pon_resource_ranges(

+                self.device_info.onu_id_start,

+                self.device_info.onu_id_end,

+                self.device_info.alloc_id_start,

+                self.device_info.alloc_id_end,

+                self.device_info.gemport_id_start,

+                self.device_info.gemport_id_end,

+                self.device_info.pon_ports

+            )

+

+        # After we have initialized resource ranges, initialize the

+        # resource pools accordingly.

+        self.resource_mgr.init_device_resource_pool()

+

+    def clear_device_resource_pool(self):

+        self.resource_mgr.clear_device_resource_pool()

diff --git a/voltha/adapters/openolt/protos/openolt.proto b/voltha/adapters/openolt/protos/openolt.proto
index 45ca664..c99814b 100644
--- a/voltha/adapters/openolt/protos/openolt.proto
+++ b/voltha/adapters/openolt/protos/openolt.proto
@@ -218,6 +218,9 @@
     fixed32 onu_id = 2;
     SerialNumber serial_number = 3;
     fixed32 pir = 4;   // peak information rate assigned to onu
+    fixed32 agg_port_id = 5;
+    fixed32 sched_id = 6;
+
 }
 
 message OmciMsg {
@@ -293,6 +296,7 @@
     string flow_type = 4;	// upstream, downstream, broadcast, multicast
     fixed32 network_intf_id = 5;
     fixed32 gemport_id = 6;
+    fixed32 alloc_id = 10;
     Classifier classifier = 7;
     Action action = 8;
     fixed32 priority = 9;