VOL-1230: Remove references to resource manager in openolt adapter as same resources are getting assigned

Change-Id: Iad1780e4c43d70d2c257bf76ae989d1bb4c50cc0
diff --git a/voltha/adapters/openolt/openolt_device.py b/voltha/adapters/openolt/openolt_device.py
index 4d2505b..2cdec29 100644
--- a/voltha/adapters/openolt/openolt_device.py
+++ b/voltha/adapters/openolt/openolt_device.py
@@ -22,7 +22,6 @@
 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
@@ -45,9 +44,7 @@
         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):
@@ -184,7 +181,6 @@
         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")
 
@@ -200,15 +196,8 @@
         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.resource_manager)
-
-        yield self._initialize_resource_manager_resource_pools()
+                                       self.logical_device_id)
 
         # TODO: check for uptime and reboot if too long (VOL-1192)
 
@@ -386,7 +375,6 @@
             # 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
@@ -410,32 +398,15 @@
             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, alloc_id, alloc_id)
+                                  serial_number_str)
             except Exception as e:
                 self.log.exception('onu-activation-failed', e=e)
 
@@ -464,12 +435,8 @@
                 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, alloc_id, alloc_id)
+                                      serial_number_str)
                 except Exception as e:
                     self.log.error('onu-activation-error',
                                    serial_number=serial_number_str, error=e)
@@ -477,7 +444,6 @@
                 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,
@@ -507,11 +473,6 @@
             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',
@@ -607,11 +568,14 @@
 
                 # tcont creation (onu)
                 tcont = TcontsConfigData()
-                tcont.alloc_id = alloc_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 = gemport_id
+                gem_port.gemport_id =  platform.mk_gemport_id(
+                    onu_indication.intf_id,
+                    onu_indication.onu_id)
 
                 gem_port.tcont_ref = str(tcont.alloc_id)
 
@@ -687,17 +651,8 @@
         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
 
-        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
-
+        onu_id = platform.onu_id_from_gemport_id(pkt_indication.gemport_id)
         logical_port_num = platform.mk_uni_port_num(pkt_indication.intf_id,
                                                     onu_id)
 
@@ -890,6 +845,19 @@
                 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')
@@ -976,10 +944,6 @@
         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()
@@ -1006,18 +970,16 @@
             self.log.info('openolt device reenabled')
 
     def activate_onu(self, intf_id, onu_id, serial_number,
-                     serial_number_str, agg_port_id, sched_id):
+                     serial_number_str):
         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,
-                              agg_port_id=agg_port_id, sched_id=sched_id)
+                              serial_number=serial_number, pir=pir)
         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,
@@ -1039,25 +1001,11 @@
             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,
-                              agg_port_id=alloc_id, sched_id=alloc_id)
+                              serial_number=serial_number)
         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:
@@ -1079,13 +1027,3 @@
     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 8b57c6f..463ce68 100644
--- a/voltha/adapters/openolt/openolt_flow_mgr.py
+++ b/voltha/adapters/openolt/openolt_flow_mgr.py
@@ -16,7 +16,6 @@
 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, \
@@ -24,7 +23,6 @@
 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
 
@@ -73,7 +71,7 @@
 
 class OpenOltFlowMgr(object):
 
-    def __init__(self, log, stub, device_id, logical_device_id, resource_mgr):
+    def __init__(self, log, stub, device_id, logical_device_id):
         self.log = log
         self.stub = stub
         self.device_id = device_id
@@ -83,7 +81,6 @@
         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)
@@ -239,7 +236,6 @@
             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):
 
@@ -249,7 +245,7 @@
         if IP_PROTO in classifier:
             if classifier[IP_PROTO] == 17:
                 self.log.debug('dhcp flow add')
-                yield self.add_dhcp_trap(intf_id, onu_id, classifier,
+                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')
@@ -260,30 +256,29 @@
         elif ETH_TYPE in classifier:
             if classifier[ETH_TYPE] == EAP_ETH_TYPE:
                 self.log.debug('eapol flow add')
-                yield self.add_eapol_flow(intf_id, onu_id, flow)
+                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:
-                    yield self.add_eapol_flow(
+                    self.add_eapol_flow(
                         intf_id, onu_id, flow,
                         eapol_id=EAPOL_SECONDARY_FLOW_INDEX,
                         vlan_id=vlan_id)
             if classifier[ETH_TYPE] == LLDP_ETH_TYPE:
                 self.log.debug('lldp flow add')
-                yield self.add_lldp_flow(flow)
+                self.add_lldp_flow(flow)
 
         elif PUSH_VLAN in action:
-            yield self.add_upstream_data_flow(intf_id, onu_id, classifier,
+            self.add_upstream_data_flow(intf_id, onu_id, classifier,
                                             action,
                                       flow)
         elif POP_VLAN in action:
-            yield self.add_downstream_data_flow(intf_id, onu_id, classifier,
+            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):
 
@@ -291,20 +286,18 @@
         uplink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG
 
 
-        yield self.add_hsia_flow(intf_id, onu_id, uplink_classifier,
+        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:
-
-            yield self.add_eapol_flow(intf_id, onu_id, eap_logical_flow,
+            self.add_eapol_flow(intf_id, onu_id, eap_logical_flow,
                 eapol_id=EAPOL_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[PACKET_TAG_TYPE] = DOUBLE_TAG
@@ -312,7 +305,7 @@
         downlink_action[POP_VLAN] = True
         downlink_action[VLAN_VID] = downlink_classifier[VLAN_VID]
 
-        yield self.add_hsia_flow(intf_id, onu_id, downlink_classifier,
+        self.add_hsia_flow(intf_id, onu_id, downlink_classifier,
                            downlink_action, DOWNSTREAM, HSIA_FLOW_INDEX,
                            flow)
 
@@ -320,18 +313,11 @@
     # 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):
 
-        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
-                     )
-
+        gemport_id = platform.mk_gemport_id(intf_id, onu_id)
+        alloc_id = platform.mk_alloc_id(intf_id, onu_id)
         flow_id = platform.mk_flow_id(intf_id, onu_id, hsia_id)
 
         flow = openolt_pb2.Flow(
@@ -344,7 +330,6 @@
 
         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,
@@ -355,13 +340,8 @@
         classifier[PACKET_TAG_TYPE] = SINGLE_TAG
         classifier.pop(VLAN_VID, None)
 
-        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
-                     )
+        gemport_id = platform.mk_gemport_id(intf_id, onu_id)
+        alloc_id = platform.mk_alloc_id(intf_id, onu_id)
         flow_id = platform.mk_flow_id(intf_id, onu_id, DHCP_FLOW_INDEX)
 
         upstream_flow = openolt_pb2.Flow(
@@ -400,7 +380,6 @@
 
         self.add_flow_to_device(downstream_flow, downstream_logical_flow)
 
-    @inlineCallbacks
     def add_eapol_flow(self, intf_id, onu_id, logical_flow,
                        eapol_id=EAPOL_FLOW_INDEX,
                        vlan_id=DEFAULT_MGMT_VLAN):
@@ -413,16 +392,10 @@
         uplink_action = {}
         uplink_action[TRAP_TO_HOST] = True
 
-        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.
 
+        gemport_id = platform.mk_gemport_id(intf_id, onu_id)
+        alloc_id = platform.mk_alloc_id(intf_id, onu_id)
         uplink_flow_id = platform.mk_flow_id(intf_id, onu_id, eapol_id)
 
         upstream_flow = openolt_pb2.Flow(
@@ -499,7 +472,6 @@
 
     """ Add a downstream LLDP trap flow on the NNI interface
     """
-    @inlineCallbacks
     def add_lldp_flow(self, logical_flow, network_intf_id=0):
 
         classifier = {}
@@ -700,4 +672,4 @@
         if id >> 15 == 0x1:
             return (id & 0x7fff, UPSTREAM)
         else:
-            return (id, DOWNSTREAM)
\ No newline at end of file
+            return (id, DOWNSTREAM)
diff --git a/voltha/adapters/openolt/openolt_platform.py b/voltha/adapters/openolt/openolt_platform.py
index b179d90..5e091f6 100644
--- a/voltha/adapters/openolt/openolt_platform.py
+++ b/voltha/adapters/openolt/openolt_platform.py
@@ -20,6 +20,33 @@
 """
 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
 
@@ -78,6 +105,18 @@
 
 MAX_ONUS_PER_PON = 112
 
+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_uni_port_num(intf_id, onu_id):
     return intf_id << 11 | onu_id << 4
 
diff --git a/voltha/adapters/openolt/openolt_resource_manager.py b/voltha/adapters/openolt/openolt_resource_manager.py
deleted file mode 100644
index a85cbc5..0000000
--- a/voltha/adapters/openolt/openolt_resource_manager.py
+++ /dev/null
@@ -1,276 +0,0 @@
-#

-# 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()