VOL-669: OpenOLT - code reorg of platform specific and flow functions

Change-Id: I75a56a98e5dc3fe0149983bd6da7810f5b286bea
diff --git a/voltha/adapters/openolt/openolt_device.py b/voltha/adapters/openolt/openolt_device.py
index 9877930..df16728 100644
--- a/voltha/adapters/openolt/openolt_device.py
+++ b/voltha/adapters/openolt/openolt_device.py
@@ -38,6 +38,8 @@
 from voltha.protos.bbf_fiber_tcont_body_pb2 import TcontsConfigData
 import voltha.core.flow_decomposer as fd
 
+import openolt_platform as platform
+
 HSIA_FLOW_INDEX = 0 # FIXME
 DHCP_FLOW_INDEX = 1 # FIXME
 EAPOL_FLOW_INDEX = 2 # FIXME
@@ -50,82 +52,6 @@
 OnuRec = collections.namedtuple('OnuRec', ['serial_number', 'state'])
 
 """
-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
-    Flow Id is unique per OLT
-    Multiple GEM ports can map to same flow id
-
-     13    11              4      0
-    +--------+--------------+------+
-    | pon id |    onu id    | Flow |
-    |        |              | idx  |
-    +--------+--------------+------+
-
-    14 bits = 16384 flows (per OLT).
-
-    pon id = 4 bits = 16 PON ports
-    onu id = 7 bits = 128 ONUss per PON port
-    Flow index = 3 bits = 4 bi-directional flows per ONU
-                        = 8 uni-directional flows per ONU
-
-
-Logical (OF) UNI port number
-
-    OpenFlow port number corresponding to PON UNI
-
-     15       11              4      0
-    +--+--------+--------------+------+
-    |0 | pon id |    onu id    |   0  |
-    +--+--------+--------------+------+
-
-    pon id = 4 bits = 16 PON ports
-    onu id = 7 bits = 128 ONUs per PON port
-
-
-PON OLT (OF) port number
-
-    OpenFlow port number corresponding to PON OLT ports
-
-     31    28                                 0
-    +--------+------------------------~~~------+
-    |  0x2   |          pon intf id            |
-    +--------+------------------------~~~------+
-
-"""
-
-"""
 OpenoltDevice represents an OLT.
 """
 class OpenoltDevice(object):
@@ -278,7 +204,7 @@
             onu_id = self.new_onu_id(intf_id)
             try:
                 self.add_onu_device(intf_id,
-                        self.intf_id_to_port_no(intf_id, Port.PON_OLT),
+                        platform.intf_id_to_port_no(intf_id, Port.PON_OLT),
                         onu_id, serial_number)
             except Exception as e:
                 self.log.exception('onu activation failed', e=e)
@@ -299,15 +225,6 @@
 	        self.log.info("ignore onu discovery indication", intf_id=intf_id,
                         onu_id=onu_id, state=self.onus[key].state)
 
-    def mk_uni_port_num(self, intf_id, onu_id):
-        return intf_id << 11 | onu_id << 4
-
-    def onu_id_from_port_num(self, port_num):
-        return (port_num >> 4) & 0x7F
-
-    def intf_id_from_port_num(self, port_num):
-        return (port_num >> 11) & 0xF
-
     def onu_indication(self, onu_indication):
         self.log.debug("onu indication", intf_id=onu_indication.intf_id,
                 onu_id=onu_indication.onu_id)
@@ -344,7 +261,7 @@
         #
         # tcont create (onu)
         #
-        alloc_id = self.mk_alloc_id(onu_indication.onu_id)
+        alloc_id = platform.mk_alloc_id(onu_indication.onu_id)
         msg = {'proxy_address':onu_device.proxy_address,
                'event':'create-tcont',
                'event_data':{'alloc_id':alloc_id}}
@@ -353,7 +270,7 @@
         #
         # v_enet create (olt)
         #
-        uni_no = self.mk_uni_port_num(onu_indication.intf_id, onu_indication.onu_id)
+        uni_no = platform.mk_uni_port_num(onu_indication.intf_id, onu_indication.onu_id)
         uni_name = self.port_name(uni_no, Port.ETHERNET_UNI,
                 serial_number=onu_indication.serial_number)
 	self.adapter_agent.add_port(
@@ -376,24 +293,12 @@
         #
         # gem port create
         #
-        gemport_id = self.mk_gemport_id(onu_indication.onu_id)
+        gemport_id = platform.mk_gemport_id(onu_indication.onu_id)
         msg = {'proxy_address':onu_device.proxy_address,
                'event':'create-gemport',
                'event_data':{'gemport_id':gemport_id}}
         self.adapter_agent.publish_inter_adapter_message(onu_device.id, msg)
 
-    def mk_gemport_id(self, onu_id, idx=0):
-        return 1<<10 | onu_id<<3 | idx
-
-    def onu_id_from_gemport_id(self, gemport_id):
-        return (gemport_id & ~(1<<10)) >> 3
-
-    def mk_alloc_id(self, 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
-
-
     def omci_indication(self, omci_indication):
 
         self.log.debug("omci indication", intf_id=omci_indication.intf_id,
@@ -411,8 +316,8 @@
                 gemport_id=pkt_indication.gemport_id,
                 flow_id=pkt_indication.flow_id)
 
-        onu_id = self.onu_id_from_gemport_id(pkt_indication.gemport_id)
-        logical_port_num = self.mk_uni_port_num(pkt_indication.intf_id, onu_id)
+        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)
 
         pkt = Ether(pkt_indication.pkt)
         kw = dict(logical_device_id=self.logical_device_id,
@@ -441,8 +346,8 @@
 
         send_pkt = binascii.unhexlify(str(payload).encode("HEX"))
 
-        onu_pkt = openolt_pb2.OnuPacket(intf_id=self.intf_id_from_port_num(egress_port),
-                onu_id=self.onu_id_from_port_num(egress_port), pkt=send_pkt)
+        onu_pkt = openolt_pb2.OnuPacket(intf_id=platform.intf_id_from_port_num(egress_port),
+                onu_id=platform.onu_id_from_port_num(egress_port), pkt=send_pkt)
 
         self.stub.OnuPacketOut(onu_pkt)
 
@@ -469,16 +374,6 @@
                 proxy_address=proxy_address, root=True,
                 serial_number=serial_number_str, admin_state=AdminState.ENABLED)
 
-    def intf_id_to_port_no(self, intf_id, intf_type):
-        if intf_type is Port.ETHERNET_NNI:
-            # FIXME - Remove hardcoded '129'
-            return intf_id + 129
-        elif intf_type is Port.PON_OLT:
-            return 0x2<<28 | intf_id
-        else:
-	    raise Exception('Invalid port type')
-
-
     def port_name(self, port_no, port_type, intf_id=None, serial_number=None):
         if port_type is Port.ETHERNET_NNI:
             return "nni" "-" + str(port_no)
@@ -510,7 +405,7 @@
         self.adapter_agent.add_logical_port(self.logical_device_id, logical_port)
 
     def add_port(self, intf_id, port_type, oper_status):
-        port_no = self.intf_id_to_port_no(intf_id, port_type)
+        port_no = platform.intf_id_to_port_no(intf_id, port_type)
 
         label = self.port_name(port_no, port_type, intf_id)
 
@@ -678,8 +573,8 @@
 
                 # FIXME - Why ignore downstream flows?
                 if is_down_stream is False:
-                    intf_id = self.intf_id_from_port_num(classifier_info['in_port'])
-                    onu_id = self.onu_id_from_port_num(classifier_info['in_port'])
+                    intf_id = platform.intf_id_from_port_num(classifier_info['in_port'])
+                    onu_id = platform.onu_id_from_port_num(classifier_info['in_port'])
                     self.divide_and_add_flow(intf_id, onu_id, classifier_info, action_info)
                 #else:
                 #    self.log.info('ignore downstream flow', flow=flow,
@@ -707,9 +602,6 @@
                 self.log.info('eapol flow add')
                 self.add_eapol_flow(intf_id, onu_id, classifier, action)
         elif 'push_vlan' in action:
-            # FIXME - Is this required?
-            #self.prepare_and_add_eapol_flow(intf_id, onu_id, classifier, action)
-            #        EAPOL_FLOW_INDEX_DATA_VLAN, EAPOL_DOWNLINK_FLOW_INDEX_DATA_VLAN)
             self.add_data_flow(intf_id, onu_id, classifier, action)
         else:
             self.log.info('Invalid-flow-type-to-handle', classifier=classifier,
@@ -787,8 +679,8 @@
     def add_hsia_flow(self, intf_id, onu_id, uplink_classifier, uplink_action,
                 downlink_classifier, downlink_action, hsia_id):
 
-        gemport_id = self.mk_gemport_id(onu_id)
-        flow_id = self.mk_flow_id(intf_id, onu_id, hsia_id)
+        gemport_id = platform.mk_gemport_id(onu_id)
+        flow_id = platform.mk_flow_id(intf_id, onu_id, hsia_id)
 
         self.log.info('add upstream flow', onu_id=onu_id, classifier=uplink_classifier,
                 action=uplink_action, gemport_id=gemport_id, flow_id=flow_id)
@@ -822,8 +714,8 @@
         classifier['pkt_tag_type'] = 'single_tag'
         classifier.pop('vlan_vid', None)
 
-        gemport_id = self.mk_gemport_id(onu_id)
-        flow_id = self.mk_flow_id(intf_id, onu_id, DHCP_FLOW_INDEX)
+        gemport_id = platform.mk_gemport_id(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",
@@ -842,8 +734,8 @@
         downlink_classifier = dict(uplink_classifier)
         downlink_action = dict(uplink_action)
 
-        gemport_id = self.mk_gemport_id(onu_id)
-        uplink_flow_id = self.mk_flow_id(intf_id, onu_id, uplink_eapol_id)
+        gemport_id = platform.mk_gemport_id(onu_id)
+        uplink_flow_id = platform.mk_flow_id(intf_id, onu_id, uplink_eapol_id)
 
         # Add Upstream EAPOL Flow.
         uplink_classifier['pkt_tag_type'] = 'single_tag'
@@ -859,7 +751,7 @@
         self.stub.FlowAdd(upstream_flow)
 
         # Add Downstream EAPOL Flow.
-        downlink_flow_id = self.mk_flow_id(intf_id, onu_id, downlink_eapol_id)
+        downlink_flow_id = platform.mk_flow_id(intf_id, onu_id, downlink_eapol_id)
         downlink_classifier['pkt_tag_type'] = 'single_tag'
         downlink_classifier['vlan_vid'] = vlan_id
 
@@ -869,6 +761,3 @@
                 action=self.mk_action(downlink_action))
 
         self.stub.FlowAdd(downstream_flow)
-
-    def mk_flow_id(self, intf_id, onu_id, idx):
-        return intf_id<<11 | onu_id<<4  | idx
diff --git a/voltha/adapters/openolt/openolt_platform.py b/voltha/adapters/openolt/openolt_platform.py
new file mode 100644
index 0000000..841daaf
--- /dev/null
+++ b/voltha/adapters/openolt/openolt_platform.py
@@ -0,0 +1,125 @@
+#
+# 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.
+#
+
+from voltha.protos.device_pb2 import Port
+
+"""
+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
+    Flow Id is unique per OLT
+    Multiple GEM ports can map to same flow id
+
+     13    11              4      0
+    +--------+--------------+------+
+    | pon id |    onu id    | Flow |
+    |        |              | idx  |
+    +--------+--------------+------+
+
+    14 bits = 16384 flows (per OLT).
+
+    pon id = 4 bits = 16 PON ports
+    onu id = 7 bits = 128 ONUss per PON port
+    Flow index = 3 bits = 4 bi-directional flows per ONU
+                        = 8 uni-directional flows per ONU
+
+
+Logical (OF) UNI port number
+
+    OpenFlow port number corresponding to PON UNI
+
+     15       11              4      0
+    +--+--------+--------------+------+
+    |0 | pon id |    onu id    |   0  |
+    +--+--------+--------------+------+
+
+    pon id = 4 bits = 16 PON ports
+    onu id = 7 bits = 128 ONUs per PON port
+
+
+PON OLT (OF) port number
+
+    OpenFlow port number corresponding to PON OLT ports
+
+     31    28                                 0
+    +--------+------------------------~~~------+
+    |  0x2   |          pon intf id            |
+    +--------+------------------------~~~------+
+
+"""
+
+def mk_uni_port_num(intf_id, onu_id):
+    return intf_id << 11 | onu_id << 4
+
+def mk_alloc_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
+
+def mk_gemport_id(onu_id, idx=0):
+    return 1<<10 | onu_id<<3 | idx
+
+def onu_id_from_gemport_id(gemport_id):
+    return (gemport_id & ~(1<<10)) >> 3
+
+def mk_flow_id(intf_id, onu_id, idx):
+    return intf_id<<11 | onu_id<<4  | idx
+
+def onu_id_from_port_num(port_num):
+    return (port_num >> 4) & 0x7F
+
+def intf_id_from_port_num(port_num):
+    return (port_num >> 11) & 0xF
+
+def intf_id_to_port_no(intf_id, intf_type):
+    if intf_type is Port.ETHERNET_NNI:
+        # FIXME - Remove hardcoded '129'
+        return intf_id + 129
+    elif intf_type is Port.PON_OLT:
+        return 0x2<<28 | intf_id
+    else:
+        raise Exception('Invalid port type')