Make ponsim_olt work for asfvolt16_olt adapter.

This is the first commit where olt activate is tested.

Change-Id: Ifa3967d8650741db8feeeff1a271618408ea4da1
diff --git a/ponsim/bal_servicer.py b/ponsim/bal_servicer.py
new file mode 100644
index 0000000..f036d67
--- /dev/null
+++ b/ponsim/bal_servicer.py
@@ -0,0 +1,74 @@
+#
+# Copyright 2017 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 structlog
+from common.utils.grpc_utils import twisted_async
+from voltha.protos import third_party
+from voltha.protos.ponsim_pb2 import PonSimDeviceInfo
+from google.protobuf.empty_pb2 import Empty
+from voltha.adapters.asfvolt16_olt.protos.bal_pb2 import BalServicer, BalErr
+from voltha.adapters.asfvolt16_olt.protos.bal_errno_pb2 import BAL_ERR_OK
+
+_ = third_party
+
+log = structlog.get_logger()
+
+class BalHandler(BalServicer):
+
+    def __init__(self, thread_pool, ponsim):
+        self.thread_pool = thread_pool
+        self.ponsim = ponsim
+
+    @twisted_async
+    def GetDeviceInfo(self, request, context):
+        log.info('get-device-info')
+        ports = self.ponsim.get_ports()
+        return PonSimDeviceInfo(
+            nni_port=ports[0],
+            uni_ports=ports[1:]
+        )
+
+    @twisted_async
+    def UpdateFlowTable(self, request, context):
+        log.info('flow-table-update', request=request, port=request.port)
+        if request.port == 0:
+            # by convention this is the olt port
+            self.ponsim.olt_install_flows(request.flows)
+        else:
+            self.ponsim.onu_install_flows(request.port, request.flows)
+        return Empty()
+
+    def GetStats(self, request, context):
+        return self.ponsim.get_stats()
+
+    def BalApiInit(self, request, context):
+        log.info('olt-connection-successful', request=request)
+        return BalErr(err=BAL_ERR_OK)
+
+    def BalApiFinish(self, request, context):
+        log.info('BalApi', request=request)
+        return BalErr(err=BAL_ERR_OK)
+
+    def BalCfgSet(self, request, context):
+        log.info('olt-activation-successful', request=request)
+        return BalErr(err=BAL_ERR_OK)
+
+    def BalAccessTerminalCfgSet(self, request, context):
+        log.info('olt-activation-successful', request=request)
+        return BalErr(err=BAL_ERR_OK)
+
+    def BalCfgClear(self, request, context):
+        log.info('BalCfClear', request=request)
+        return BalErr(err=BAL_ERR_OK)
diff --git a/ponsim/grpc_server.py b/ponsim/grpc_server.py
index fb08346..13d0ab9 100644
--- a/ponsim/grpc_server.py
+++ b/ponsim/grpc_server.py
@@ -18,65 +18,12 @@
 import os
 from concurrent import futures
 
-from common.utils.grpc_utils import twisted_async
 from voltha.protos import third_party
-from voltha.protos.ponsim_pb2 import PonSimServicer, \
-    add_PonSimServicer_to_server, PonSimDeviceInfo
-from google.protobuf.empty_pb2 import Empty
-
-from voltha.protos.ponsim_pb2 import XPonSimServicer, add_XPonSimServicer_to_server
 
 _ = third_party
 
 log = structlog.get_logger()
 
-
-class FlowUpdateHandler(PonSimServicer):
-
-    def __init__(self, thread_pool, ponsim):
-        self.thread_pool = thread_pool
-        self.ponsim = ponsim
-
-    @twisted_async
-    def GetDeviceInfo(self, request, context):
-        log.info('get-device-info')
-        ports = self.ponsim.get_ports()
-        return PonSimDeviceInfo(
-            nni_port=ports[0],
-            uni_ports=ports[1:]
-        )
-
-    @twisted_async
-    def UpdateFlowTable(self, request, context):
-        log.info('flow-table-update', request=request, port=request.port)
-        if request.port == 0:
-            # by convention this is the olt port
-            self.ponsim.olt_install_flows(request.flows)
-        else:
-            self.ponsim.onu_install_flows(request.port, request.flows)
-        return Empty()
-
-    def GetStats(self, request, context):
-        return self.ponsim.get_stats()
-
-class XPonHandler(XPonSimServicer):
-
-    def __init__(self, thread_pool, x_pon_sim):
-        self.thread_pool = thread_pool
-        self.x_pon_sim = x_pon_sim
-
-    def CreateInterface(self, request, context):
-        self.x_pon_sim.CreateInterface(request)
-        return Empty()
-
-    def UpdateInterface(self, request, context):
-        self.x_pon_sim.UpdateInterface(request)
-        return Empty()
-
-    def RemoveInterface(self, request, context):
-        self.x_pon_sim.RemoveInterface(request)
-        return Empty()
-
 class GrpcServer(object):
 
     def __init__(self, port, ponsim, x_pon_sim):
@@ -86,12 +33,16 @@
         self.ponsim = ponsim
         self.x_pon_sim = x_pon_sim
 
-    def start(self):
+    '''
+    service_list: a list of (add_xyzSimServicer_to_server, xyzServicerClass)
+    e.g. [(add_PonSimServicer_to_server, FlowUpdateHandler),
+          (add_XPonSimServicer_to_server, XPonHandler)]
+    '''
+    def start(self, service_list):
         log.debug('starting')
-        handler = FlowUpdateHandler(self.thread_pool, self.ponsim)
-        add_PonSimServicer_to_server(handler, self.server)
-        x_pon_handler = XPonHandler(self.thread_pool, self.x_pon_sim)
-        add_XPonSimServicer_to_server(x_pon_handler, self.server)
+        for add_x_to_server, xServiceClass in service_list:
+            x_handler = xServiceClass(self.thread_pool, self.ponsim)
+            add_x_to_server(x_handler, self.server)
 
         # read in key and certificate
         try:
@@ -108,7 +59,6 @@
         # create server credentials
         server_credentials = grpc.ssl_server_credentials(((private_key, certificate_chain,),))
         self.server.add_secure_port('[::]:%s' % self.port, server_credentials)
-
         self.server.start()
         log.info('started')
 
diff --git a/ponsim/main.py b/ponsim/main.py
index 3140afd..63b47f6 100755
--- a/ponsim/main.py
+++ b/ponsim/main.py
@@ -28,16 +28,21 @@
 
 from common.structlog_setup import setup_logging
 from grpc_server import GrpcServer
-from ponsim import PonSim
 from realio import RealIo
-
+from voltha.protos.ponsim_pb2 import add_PonSimServicer_to_server
+from voltha.protos.ponsim_pb2 import add_XPonSimServicer_to_server
+from voltha.adapters.asfvolt16_olt.protos.bal_pb2 import add_BalServicer_to_server
+import ponsim_servicer
+import bal_servicer
+from ponsim import PonSim
 from ponsim import XPonSim
 
 defs = dict(
     config=os.environ.get('CONFIG', './ponsim.yml'),
     grpc_port=int(os.environ.get('GRPC_PORT', 50060)),
     name=os.environ.get('NAME', 'pon1'),
-    onus=int(os.environ.get("ONUS", 1))
+    onus=int(os.environ.get("ONUS", 1)),
+    device_type='ponsim'
 )
 
 
@@ -139,6 +144,14 @@
                         default=False,
                         help=_help)
 
+    _help = ('device type - ponsim or bal'
+             ' (default: %s)' % defs['device_type'])
+    parser.add_argument('-d', '--device_type',
+                        dest='device_type',
+                        action='store',
+                        default=defs['device_type'],
+                        help=_help)
+
     args = parser.parse_args()
 
     return args
@@ -169,13 +182,19 @@
         if not args.no_banner:
             print_banner(self.log)
 
-        self.startup_components()
+        if args.device_type == 'ponsim':
+            grpc_services =  [(add_PonSimServicer_to_server, ponsim_servicer.FlowUpdateHandler)]
+        elif args.device_type == 'bal':
+            grpc_services =  [(add_BalServicer_to_server, bal_servicer.BalHandler)]
+        grpc_services.append((add_XPonSimServicer_to_server, ponsim_servicer.XPonHandler))
+
+        self.startup_components(grpc_services)
 
     def start(self):
         self.start_reactor()  # will not return except Keyboard interrupt
 
     @inlineCallbacks
-    def startup_components(self):
+    def startup_components(self, grpc_services):
         try:
             self.log.info('starting-internal-components')
 
@@ -188,7 +207,7 @@
             self.x_pon_sim = XPonSim()
 
             self.grpc_server = GrpcServer(self.args.grpc_port, self.ponsim, self.x_pon_sim)
-            yield self.grpc_server.start()
+            yield self.grpc_server.start(grpc_services)
 
             self.log.info('started-internal-services')
 
diff --git a/ponsim/ponsim_servicer.py b/ponsim/ponsim_servicer.py
new file mode 100644
index 0000000..b7a524f
--- /dev/null
+++ b/ponsim/ponsim_servicer.py
@@ -0,0 +1,71 @@
+#
+# Copyright 2017 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 structlog
+from common.utils.grpc_utils import twisted_async
+from voltha.protos import third_party
+from voltha.protos.ponsim_pb2 import PonSimServicer, PonSimDeviceInfo
+from google.protobuf.empty_pb2 import Empty
+from voltha.protos.ponsim_pb2 import XPonSimServicer
+
+_ = third_party
+
+log = structlog.get_logger()
+
+class FlowUpdateHandler(PonSimServicer):
+
+    def __init__(self, thread_pool, ponsim):
+        self.thread_pool = thread_pool
+        self.ponsim = ponsim
+
+    @twisted_async
+    def GetDeviceInfo(self, request, context):
+        log.info('get-device-info')
+        ports = self.ponsim.get_ports()
+        return PonSimDeviceInfo(
+            nni_port=ports[0],
+            uni_ports=ports[1:]
+        )
+
+    @twisted_async
+    def UpdateFlowTable(self, request, context):
+        log.info('flow-table-update', request=request, port=request.port)
+        if request.port == 0:
+            # by convention this is the olt port
+            self.ponsim.olt_install_flows(request.flows)
+        else:
+            self.ponsim.onu_install_flows(request.port, request.flows)
+        return Empty()
+
+    def GetStats(self, request, context):
+        return self.ponsim.get_stats()
+
+class XPonHandler(XPonSimServicer):
+
+    def __init__(self, thread_pool, x_pon_sim):
+        self.thread_pool = thread_pool
+        self.x_pon_sim = x_pon_sim
+
+    def CreateInterface(self, request, context):
+        self.x_pon_sim.CreateInterface(request)
+        return Empty()
+
+    def UpdateInterface(self, request, context):
+        self.x_pon_sim.UpdateInterface(request)
+        return Empty()
+
+    def RemoveInterface(self, request, context):
+        self.x_pon_sim.RemoveInterface(request)
+        return Empty()
diff --git a/tests/itests/voltha/adapters/asfvolt16_olt/test_device_state_changes.py b/tests/itests/voltha/adapters/asfvolt16_olt/test_device_state_changes.py
index b3fb0d5..89edc54 100644
--- a/tests/itests/voltha/adapters/asfvolt16_olt/test_device_state_changes.py
+++ b/tests/itests/voltha/adapters/asfvolt16_olt/test_device_state_changes.py
@@ -14,17 +14,17 @@
     The prerequisite for this test are:
      1. voltha ensemble is running
           docker-compose -f compose/docker-compose-system-test.yml up -d
-     2.  asfvolt16_olt simulator is running
+     2. ponsim olt is running with 1 OLT and 4 ONUs using device_type 'bal'
           sudo -s
           . ./env.sh
-          ./voltha/adapters/asfvolt16_olt/sim.py
+          ./ponsim/main.py -v -o 4 -d bal
     """
 
     # Retrieve details of the REST entry point
     rest_endpoint = get_endpoint_from_consul(LOCAL_CONSUL, 'chameleon-rest')
 
     # Construct the base_url
-    base_url = 'http://' + rest_endpoint
+    base_url = 'https://' + rest_endpoint
 
     def wait_till(self, msg, predicate, interval=0.1, timeout=5.0):
         deadline = time() + timeout
diff --git a/voltha/adapters/asfvolt16_olt/asfvolt16_device_handler.py b/voltha/adapters/asfvolt16_olt/asfvolt16_device_handler.py
index 2ca9d81..e0563e7 100644
--- a/voltha/adapters/asfvolt16_olt/asfvolt16_device_handler.py
+++ b/voltha/adapters/asfvolt16_olt/asfvolt16_device_handler.py
@@ -19,7 +19,7 @@
 """
 
 from uuid import uuid4
-
+from common.frameio.frameio import BpfProgramFilter
 from voltha.protos.common_pb2 import OperStatus, ConnectStatus
 from voltha.protos.device_pb2 import Port
 from voltha.protos.common_pb2 import AdminState
@@ -31,18 +31,33 @@
 from voltha.adapters.asfvolt16_olt.bal import Bal
 from voltha.adapters.device_handler import OltDeviceHandler
 
+# TODO: VLAN ID needs to come from some sort of configuration.
+PACKET_IN_VLAN = 4091
+is_inband_frame = BpfProgramFilter('(ether[14:2] & 0xfff) = 0x{:03x}'.format(
+    PACKET_IN_VLAN))
+
+#TODO: hardcoded NNI port ID to be removed once port enumeration is supported.
+nni_port_no = 1
+
+# TODO - hardcoded OLT ID to be removed once multiple OLT devices is supported.
+olt_id = 1
+
 class Asfvolt16Handler(OltDeviceHandler):
     def __init__(self, adapter, device_id):
         super(Asfvolt16Handler, self).__init__(adapter, device_id)
+        self.filter = is_inband_frame
         self.bal = Bal(self.log)
+        self.host_and_port = None
+
+    def __del__(self):
+        super(Asfvolt16Handler, self).__del__()
+
+    def __str__(self):
+        return "Asfvolt16Handler: {}".format(self.host_and_port)
 
     def activate(self, device):
-        '''
-        TODO: Revisit how to determine the NNI port number.
-        Hardcoding for now.
-        '''
-        port_no = 1
-        self.log.info('activating-olt', device=device)
+
+        self.log.info('activating-asfvolt16-olt', device=device)
 
         if self.logical_device_id is not None:
             return
@@ -53,24 +68,23 @@
             self.adapter_agent.update_device(device)
             return
 
+        self.host_and_port = device.host_and_port
+
         device.root = True
         device.vendor = 'Edgecore'
         device.model = 'ASFvOLT16'
         device.serial_number = device.host_and_port
         self.adapter_agent.update_device(device)
 
-        self.add_port(port_no=port_no, port_type=Port.ETHERNET_NNI)
+        self.add_port(port_no=nni_port_no, port_type=Port.ETHERNET_NNI)
         self.logical_device_id = self.add_logical_device(device_id=device.id)
-        self.add_logical_port(port_no=port_no,
+        self.add_logical_port(port_no=nni_port_no,
                               port_type=Port.ETHERNET_NNI,
                               device_id=device.id,
                               logical_device_id=self.logical_device_id)
 
         self.bal.connect_olt(device.host_and_port)
-        # TODO - Add support for multiple OLT devices.
-        # Only single OLT is supported currently and it id is
-        # hard-coded to 0.
-        self.bal.activate_olt(olt_id=0)
+        self.bal.activate_olt(olt_id)
 
         device = self.adapter_agent.get_device(device.id)
         device.parent_id = self.logical_device_id
@@ -78,6 +92,9 @@
         device.oper_status = OperStatus.ACTIVATING
         self.adapter_agent.update_device(device)
 
+        # Open the frameio port to receive in-band packet_in messages
+        self.activate_io_port()
+
     def add_port(self, port_no, port_type):
         self.log.info('adding-port', port_no=port_no, port_type=port_type)
         if port_type is Port.ETHERNET_NNI:
diff --git a/voltha/adapters/device_handler.py b/voltha/adapters/device_handler.py
index a3bc0a6..33bbd35 100644
--- a/voltha/adapters/device_handler.py
+++ b/voltha/adapters/device_handler.py
@@ -14,6 +14,8 @@
 # limitations under the License.
 #
 import structlog
+from scapy.layers.l2 import Ether, Dot1Q
+from voltha.registry import registry
 from voltha.protos.common_pb2 import OperStatus, ConnectStatus, AdminState
 
 class DeviceHandler(object):
@@ -41,6 +43,11 @@
 class OltDeviceHandler(DeviceHandler):
     def __init__(self, adapter, device_id):
         super(OltDeviceHandler, self).__init__(adapter, device_id)
+        self.filter = None
+
+    def __del__(self):
+        if self.io_port is not None:
+            registry('frameio').close_port(self.io_port)
 
     def disable(self):
         super(OltDeviceHandler, self).disable()
@@ -81,5 +88,45 @@
 
         self.log.info('deleted', device_id=self.device_id)
 
+    def activate_io_port(self):
+        if self.io_port is None:
+            self.log.info('registering-frameio')
+            self.io_port = registry('frameio').open_port(
+                self.interface, self.rcv_io, self.filter)
+
+    def deactivate_io_port(self):
+        io, self.io_port = self.io_port, None
+
+        if io is not None:
+            registry('frameio').close_port(io)
+
+    def rcv_io(self, port, frame):
+        self.log.info('received', iface_name=port.iface_name,
+                      frame_len=len(frame))
+        pkt = Ether(frame)
+        if pkt.haslayer(Dot1Q):
+            outer_shim = pkt.getlayer(Dot1Q)
+            if isinstance(outer_shim.payload, Dot1Q):
+                inner_shim = outer_shim.payload
+                cvid = inner_shim.vlan
+                logical_port = cvid
+                popped_frame = (
+                    Ether(src=pkt.src, dst=pkt.dst, type=inner_shim.type) /
+                    inner_shim.payload
+                )
+                kw = dict(
+                    logical_device_id=self.logical_device_id,
+                    logical_port_no=logical_port,
+                )
+                self.log.info('sending-packet-in', **kw)
+                self.adapter_agent.send_packet_in(
+                    packet=str(popped_frame), **kw)
+            '''
+            # TODO: handle non dot1q pkts
+            elif pkt.haslayer(Raw):
+                raw_data = json.loads(pkt.getlayer(Raw).load)
+                self.alarms.send_alarm(self, raw_data)
+            '''
+
 class OnuDeviceHandler(DeviceHandler):
     pass