VOL-1452: Move logical port callback functions

Needed to inject device and port specific info
contained in handler

Change-Id: If7368514de8809a73f0034a34479a84744d28e02
diff --git a/python/adapters/openolt/openolt.py b/python/adapters/openolt/openolt.py
index 5d55e79..682b62c 100644
--- a/python/adapters/openolt/openolt.py
+++ b/python/adapters/openolt/openolt.py
@@ -17,37 +17,15 @@
 """
 Openolt adapter.
 """
-import arrow
-import grpc
 import structlog
 
 from zope.interface import implementer
-from google.protobuf.empty_pb2 import Empty
-from google.protobuf.json_format import MessageToDict
-from scapy.layers.inet import Raw
-import json
-from google.protobuf.message import Message
-from grpc._channel import _Rendezvous
-from scapy.layers.l2 import Ether, Dot1Q
-from simplejson import dumps
 from twisted.internet import reactor
-from twisted.internet.defer import inlineCallbacks, returnValue
-from twisted.internet.task import LoopingCall
-
-from pyvoltha.adapters.common.frameio.frameio import BpfProgramFilter, hexify
 from pyvoltha.adapters.iadapter import IAdapterInterface
-from pyvoltha.common.utils.asleep import asleep
 from pyvoltha.common.utils.registry import registry
-from pyvoltha.adapters.kafka.kafka_proxy import get_kafka_proxy
-from voltha_protos import openolt_pb2
-#from voltha_protos import third_party
-from voltha_protos.common_pb2 import OperStatus, ConnectStatus
 from voltha_protos.common_pb2 import LogLevel
 from voltha_protos.common_pb2 import OperationResp
-from voltha_protos.inter_container_pb2 import SwitchCapability, PortCapability, \
-    InterAdapterMessageType, InterAdapterResponseBody
-from voltha_protos.device_pb2 import Port, PmConfig, PmConfigs, \
-    DeviceType, DeviceTypes
+from voltha_protos.device_pb2 import DeviceType, DeviceTypes
 from voltha_protos.adapter_pb2 import Adapter
 from voltha_protos.adapter_pb2 import AdapterConfig
 from openolt_flow_mgr import OpenOltFlowMgr
@@ -58,16 +36,6 @@
 from openolt_resource_manager import OpenOltResourceMgr
 from openolt_device import OpenoltDevice
 
-from voltha_protos.events_pb2 import KpiEvent, KpiEventType, MetricValuePairs
-from voltha_protos.logical_device_pb2 import LogicalPort
-from voltha_protos.openflow_13_pb2 import OFPPS_LIVE, OFPPF_FIBER, \
-    OFPPF_1GB_FD, \
-    OFPC_GROUP_STATS, OFPC_PORT_STATS, OFPC_TABLE_STATS, OFPC_FLOW_STATS, \
-    ofp_switch_features, ofp_desc
-from voltha_protos.openflow_13_pb2 import ofp_port
-from pyvoltha.common.utils.nethelpers import mac_str_to_tuple
-
-#_ = third_party
 log = structlog.get_logger()
 OpenOltDefaults = {
     'support_classes': {
@@ -110,7 +78,6 @@
         self.interface = registry('main').get_args().interface
         self.logical_device_id_to_root_device_id = dict()
         self.num_devices = 0
-        self.ofp_port_no = None
 
     def start(self):
         log.info('started', interface=self.interface)
@@ -119,50 +86,14 @@
         log.info('stopped', interface=self.interface)
 
     def get_ofp_device_info(self, device):
-        log.info('get_ofp_device_info', device_id=device.id)
-        return SwitchCapability(
-            desc=ofp_desc(
-                hw_desc='white box OLT',  # Hardware description
-                sw_desc='openolt',  # Software description
-                serial_num=device.serial_number,  # Serial number
-                dp_desc='n/a'  # Human readable description of datapath
-            ),
-            switch_features=ofp_switch_features(
-                n_buffers=256,  # Max packets buffered at once          # TODO fake for now
-                n_tables=2,  # Number of tables supported by datapath   # TODO fake for now
-                capabilities=( #Bitmap of support "ofp_capabilities"    # TODO fake for now
-                        OFPC_FLOW_STATS
-                        | OFPC_TABLE_STATS
-                        | OFPC_PORT_STATS
-                        | OFPC_GROUP_STATS
-                )
-            )
-        )
+        ofp_device_info = self.devices[device.id].get_ofp_device_info(device)
+        log.debug('get_ofp_device_info', ofp_device_info=ofp_device_info)
+        return ofp_device_info
 
     def get_ofp_port_info(self, device, port_no):
-        # Since the adapter created the device port then it has the reference of the port to
-        # return the capability.   TODO:  Do a lookup on the NNI port number and return the
-        # appropriate attributes
-        log.info('get_ofp_port_info', port_no=port_no,
-                      info=self.ofp_port_no, device_id=device.id)
-        cap = OFPPF_1GB_FD | OFPPF_FIBER
-        return PortCapability(
-            port=LogicalPort(
-                ofp_port=ofp_port(
-                    hw_addr=mac_str_to_tuple(
-                        '00:00:00:00:00:%02x' % port_no),
-                    config=0,
-                    state=OFPPS_LIVE,
-                    curr=cap,
-                    advertised=cap,
-                    peer=cap,
-                    curr_speed=OFPPF_1GB_FD,
-                    max_speed=OFPPF_1GB_FD
-                ),
-                device_id=device.id,
-                device_port_no=port_no
-            )
-        )
+        ofp_port_info = self.devices[device.id].get_ofp_port_info(device, port_no)
+        log.debug('get_ofp_port_info', device_id=device.id, ofp_port_no=ofp_port_info)
+        return ofp_port_info
 
     def adapter_descriptor(self):
         log.debug('get descriptor', interface=self.interface)
diff --git a/python/adapters/openolt/openolt_device.py b/python/adapters/openolt/openolt_device.py
index 4cba020..3242839 100644
--- a/python/adapters/openolt/openolt_device.py
+++ b/python/adapters/openolt/openolt_device.py
@@ -37,7 +37,8 @@
 from pyvoltha.common.utils.registry import registry
 from voltha_protos.common_pb2 import AdminState, OperStatus, ConnectStatus
 from voltha_protos.device_pb2 import Port, Device
-from voltha_protos.inter_container_pb2 import InterAdapterMessageType, InterAdapterOmciMessage
+from voltha_protos.inter_container_pb2 import SwitchCapability, PortCapability, \
+    InterAdapterMessageType, InterAdapterOmciMessage
 from voltha_protos.logical_device_pb2 import LogicalDevice, LogicalPort
 
 
@@ -103,6 +104,7 @@
         self.device_id = device.id
         self.host_and_port = device.host_and_port
         self.extra_args = device.extra_args
+        self.device_info = None
         self.log = structlog.get_logger(id=self.device_id,
                                         ip=self.host_and_port)
 
@@ -248,7 +250,7 @@
         delay = 1
         while True:
             try:
-                device_info = self.stub.GetDeviceInfo(openolt_pb2.Empty())
+                self.device_info = self.stub.GetDeviceInfo(openolt_pb2.Empty())
                 break
             except Exception as e:
                 reraise = True
@@ -264,12 +266,12 @@
                 if reraise:
                     raise
 
-        self.log.info('Device connected', device_info=device_info)
+        self.log.info('Device connected', device_info=self.device_info)
 
         # self.create_logical_device(device_info)
         self.logical_device_id = 0
 
-        serial_number = device_info.device_serial_number
+        serial_number = self.device_info.device_serial_number
         if serial_number is None: 
             serial_number = self.serial_number
         device.serial_number = serial_number
@@ -277,14 +279,15 @@
         self.serial_number = serial_number
 
         device.root = True
-        device.vendor = device_info.vendor
-        device.model = device_info.model
-        device.hardware_version = device_info.hardware_version
-        device.firmware_version = device_info.firmware_version
+        device.vendor = self.device_info.vendor
+        device.model = self.device_info.model
+        device.hardware_version = self.device_info.hardware_version
+        device.firmware_version = self.device_info.firmware_version
 
         # TODO: check for uptime and reboot if too long (VOL-1192)
 
         device.connect_status = ConnectStatus.REACHABLE
+        # TODO NEW CORE: Gather this from DeviceInfo proto from openolt agent
         device.mac_address = "AA:BB:CC:DD:EE:FF"
         yield self.core_proxy.device_update(device)
         
@@ -292,7 +295,7 @@
         self.resource_mgr = self.resource_mgr_class(self.device_id,
                                                     self.host_and_port,
                                                     self.extra_args,
-                                                    device_info)
+                                                    self.device_info)
         self.platform = self.platform_class(self.log, self.resource_mgr)
         self.flow_mgr = self.flow_mgr_class(self.core_proxy, self.log,
                                             self.stub, self.device_id,
@@ -878,6 +881,52 @@
 
         self.log.debug("onu-added", onu_id=onu_id, port_no=port_no, serial_number=serial_number_str)
 
+    def get_ofp_device_info(self, device):
+        self.log.info('get_ofp_device_info', device_id=device.id)
+
+        mfr_desc = self.device_info.vendor
+        sw_desc = self.device_info.firmware_version
+        hw_desc = self.device_info.model
+        if self.device_info.hardware_version: hw_desc += '-' + self.device_info.hardware_version
+
+        return SwitchCapability(
+            desc=ofp_desc(
+                hw_desc=hw_desc,
+                sw_desc=sw_desc,
+                serial_num=device.serial_number
+            ),
+            switch_features=ofp_switch_features(
+                n_buffers=256,  # Max packets buffered at once          # TODO fake for now
+                n_tables=2,  # Number of tables supported by datapath   # TODO fake for now
+                capabilities=( #Bitmap of support "ofp_capabilities"    # TODO fake for now
+                        OFPC_FLOW_STATS
+                        | OFPC_TABLE_STATS
+                        | OFPC_PORT_STATS
+                        | OFPC_GROUP_STATS
+                )
+            )
+        )
+
+    def get_ofp_port_info(self, device, port_no):
+        self.log.info('get_ofp_port_info', port_no=port_no, device_id=device.id)
+        cap = OFPPF_1GB_FD | OFPPF_FIBER
+        return PortCapability(
+            port=LogicalPort(
+                ofp_port=ofp_port(
+                    hw_addr=mac_str_to_tuple(self._get_mac_form_port_no(port_no)),
+                    config=0,
+                    state=OFPPS_LIVE,
+                    curr=cap,
+                    advertised=cap,
+                    peer=cap,
+                    curr_speed=OFPPF_1GB_FD,
+                    max_speed=OFPPF_1GB_FD
+                ),
+                device_id=device.id,
+                device_port_no=port_no
+            )
+        )
+
     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)