Leftover differences from sjmeet beanch

Change-Id: I05b5b1d92f95e2707933e68302122f8e6a5c89c2
diff --git a/voltha/adapters/maple_olt/maple_olt.py b/voltha/adapters/maple_olt/maple_olt.py
index f250ae9..559a430 100644
--- a/voltha/adapters/maple_olt/maple_olt.py
+++ b/voltha/adapters/maple_olt/maple_olt.py
@@ -14,11 +14,13 @@
 # limitations under the License.
 #
 
+import sys
 from uuid import uuid4
 
 import structlog
+from twisted.spread import pb
 from twisted.internet import reactor
-from twisted.internet.defer import inlineCallbacks
+from twisted.internet.defer import inlineCallbacks, returnValue
 from zope.interface import implementer
 
 from common.utils.asleep import asleep
@@ -37,6 +39,17 @@
 log = structlog.get_logger()
 
 
+class AsyncRx(pb.Root):
+    def remote_echo(self, pkt_type, pon, onu, port, crc_ok, msg_size, msg_data):
+        log.info('packet-type', pkt_type=pkt_type)
+        log.info('pon-id', pon_id=pon)
+        log.info('onu-id', onu_id=onu)
+        log.info('port', port_id=port)
+        log.info('crc-ok', crc_ok=crc_ok)
+        log.info('msg-size', msg_size=msg_size)
+        log.info('msg-data', msg_data="".join("{:02x}".format(ord(c)) for c in msg_data))
+        return 0
+
 @implementer(IAdapterInterface)
 class MapleOltAdapter(object):
 
@@ -59,6 +72,10 @@
             version='0.1',
             config=AdapterConfig(log_level=LogLevel.INFO)
         )
+        self.PBServerPort = 24497
+        #start PB server
+        reactor.listenTCP(self.PBServerPort, pb.PBServerFactory(AsyncRx()))
+        log.info('PB-server-started on port', port=self.PBServerPort)
 
     def start(self):
         log.debug('starting')
@@ -92,8 +109,24 @@
     def deactivate_device(self, device):
         raise NotImplementedError()
 
+    @inlineCallbacks
     def _activate_device(self, device):
 
+        # launch connecion
+        log.info('initiating-connection-to-olt', device_id=device.id, ipv4=device.ipv4_address)
+        self.pbc_factory = pb.PBClientFactory()
+        reactor.connectTCP(device.ipv4_address, 24498, self.pbc_factory)
+        self.remote = yield self.pbc_factory.getRootObject()
+        log.info('connected-to-olt', device_id=device.id, ipv4=device.ipv4_address)
+
+        data = yield self.remote.callRemote('connect_olt', 0)
+        #TODO: add error handling
+        log.info('connect-data', data=data)
+
+        data = yield self.remote.callRemote('activate_olt', 0)
+        #TODO: add error handling
+        log.info('activate-data', data=data)
+
         # first we pretend that we were able to contact the device and obtain
         # additional information about it
         device.root = True
@@ -177,7 +210,7 @@
         reactor.callLater(0.1, self._simulate_detection_of_onus, device)
 
     def _simulate_detection_of_onus(self, device):
-        for i in xrange(1, 5):
+        for i in xrange(1, 2):
             log.info('activate-olt-for-onu-{}'.format(i))
             vlan_id = self._olt_side_onu_activation(i)
             self.adapter_agent.child_device_detected(
@@ -186,11 +219,12 @@
                 child_device_type='broadcom_onu',
                 proxy_address=Device.ProxyAddress(
                     device_id=device.id,
-                    channel_id=vlan_id
+                    channel_id=i
                 ),
-                vlan=vlan_id
+                vlan=i+1024
             )
 
+    @inlineCallbacks
     def _olt_side_onu_activation(self, seq):
         """
         This is where if this was a real OLT, the OLT-side activation for
@@ -198,8 +232,51 @@
         be able to provide tunneled (proxy) communication to the given ONU,
         using the returned information.
         """
-        vlan_id = seq + 100
-        return vlan_id
+        data = yield self.remote.callRemote('create_onu', 0, seq, '4252434d', '12345678')
+        log.info('create-onu-data', data=data)
+
+        vlan_id = seq + 1024
+
+        data = yield self.remote.callRemote('configure_onu', 0, seq, alloc_id=vlan_id, unicast_gem=vlan_id, multicast_gem=4095)
+        log.info('configure-onu-data', data=data)
+
+        data = yield self.remote.callRemote('activate_onu', 0, seq)
+        log.info('activate-onu-data', data=data)
+
+        log.info('ready-to-send-omci')
+        omci_msg = "00014F0A00020000000000000000000000000000000000000000000000000000000000000000000000000028"
+        log.info('sending-omci-msg', msg=omci_msg)
+        try:
+            res = yield self.remote.callRemote(
+                             'send_omci',
+                             0,
+                             0,
+                             1,
+                             omci_msg
+                        )
+            log.info('omci-send-result', result=res)
+        except Exception, e:
+            log.info('omci-send-exception', exc=str(e))
+
+        #reactor.callLater(5.0, self._send_omci_test_msg)
+
+        returnValue(vlan_id)
+
+    @inlineCallbacks
+    def _send_omci_test_msg(self):
+        omci_msg = "00014F0A00020000000000000000000000000000000000000000000000000000000000000000000000000028"
+        log.info('sending-omci-msg', msg=omci_msg)
+        try:
+            res = yield self.remote.callRemote(
+                             'send_omci',
+                             0,
+                             0,
+                             1,
+                             omci_msg
+                        )
+            log.info('omci-send-result', result=res)
+        except Exception, e:
+            log.info('omci-send-exception', exc=str(e))
 
     def update_flows_bulk(self, device, flows, groups):
         log.debug('bulk-flow-update', device_id=device.id,
diff --git a/voltha/adapters/tibit_olt/README.md b/voltha/adapters/tibit_olt/README.md
index a454b1b..ecf2c52 100644
--- a/voltha/adapters/tibit_olt/README.md
+++ b/voltha/adapters/tibit_olt/README.md
@@ -49,7 +49,7 @@
 Issue the following command to pre-provision the Tibit OLT:
 
 ```
-curl -s -X POST -d '{"type": "tibit_olt", "mac_address": "00:00:00:00:00:01"}' \
+curl -s -X POST -d '{"type": "tibit_olt", "mac_address": "00:0c:e2:31:06:00"}' \
     http://localhost:8881/api/v1/local/devices | jq '.' | tee olt.json
 ```
 
diff --git a/voltha/adapters/tibit_olt/tibit_olt.py b/voltha/adapters/tibit_olt/tibit_olt.py
index e0bac13..a871cc9 100644
--- a/voltha/adapters/tibit_olt/tibit_olt.py
+++ b/voltha/adapters/tibit_olt/tibit_olt.py
@@ -19,6 +19,10 @@
 """
 import scapy
 import structlog
+import json
+
+from uuid import uuid4
+
 from scapy.layers.inet import ICMP, IP
 from scapy.layers.l2 import Ether, Dot1Q
 from twisted.internet.defer import DeferredQueue, inlineCallbacks
@@ -26,11 +30,14 @@
 
 from zope.interface import implementer
 
+from common.utils.asleep import asleep
+
 from common.frameio.frameio import BpfProgramFilter
 from voltha.registry import registry
 from voltha.adapters.interface import IAdapterInterface
 from voltha.core.logical_device_agent import mac_str_to_tuple
 from voltha.protos.adapter_pb2 import Adapter, AdapterConfig
+from voltha.protos.device_pb2 import Device, Port
 from voltha.protos.device_pb2 import DeviceType, DeviceTypes
 from voltha.protos.health_pb2 import HealthStatus
 from voltha.protos.common_pb2 import LogLevel, ConnectStatus
@@ -57,6 +64,7 @@
     name = "TBJSON"
     fields_desc = [StrField("data", default="")]
 
+
 @implementer(IAdapterInterface)
 class TibitOltAdapter(object):
 
@@ -141,14 +149,14 @@
 
         # if we got response, we can fill out the device info, mark the device
         # reachable
-
+        jdev = json.loads(response.data[5:])
         device.root = True
-        device.vendor = 'Tibit stuff'
-        device.model = 'n/a'
-        device.hardware_version = 'n/a'
-        device.firmware_version = 'n/a'
-        device.software_version = '1.0'
-        device.serial_number = 'add junk here'
+        device.vendor = 'Tibit Communications, Inc.'
+        device.model = jdev['results']['device']
+        device.hardware_version = jdev['results']['datecode']
+        device.firmware_version = jdev['results']['firmware']
+        device.software_version = jdev['results']['modelversion']
+        device.serial_number = jdev['results']['manufacturer']
         device.connect_status = ConnectStatus.REACHABLE
         self.adapter_agent.update_device(device)
 
@@ -273,21 +281,19 @@
 
         log.info('frame-recieved')
 
-        # extract source mac
+        # make into frame to extract source mac
         response = Ether(frame)
 
-        # enqueue incoming parsed frame to rigth device
+        # enqueue incoming parsed frame to right device
         self.incoming_queues[response.src].put(response)
 
     def _make_ping_frame(self, mac_address):
-        # TODO Nathan to make this to be an actual OLT ping
         # Create a json packet
         json_operation_str = '{\"operation\":\"version\"}'
         frame = Ether()/TBJSON(data='json %s' % json_operation_str)
         frame.type = int("9001", 16)
-        frame.dst = '00:0c:e2:31:25:00'
+        frame.dst = mac_address
         bind_layers(Ether, TBJSON, type=0x9001)
-        frame.show()
         return str(frame)
 
     def _make_links_frame(self, mac_address):