VOL-217 - Initial implementation of Adapter base class

Change-Id: Ia719374877902e804ba85f18e2005c63579ac8f3
diff --git a/voltha/adapters/asfvolt16_olt/asfvolt16_olt.py b/voltha/adapters/asfvolt16_olt/asfvolt16_olt.py
index 74848e2..ab7200e 100644
--- a/voltha/adapters/asfvolt16_olt/asfvolt16_olt.py
+++ b/voltha/adapters/asfvolt16_olt/asfvolt16_olt.py
@@ -18,109 +18,14 @@
 Asfvolt16 OLT adapter
 """
 
-import structlog
-from zope.interface import implementer
+from voltha.adapters.iadapter import IAdapter
 
-from voltha.protos.device_pb2 import DeviceType, DeviceTypes
-from voltha.adapters.interface import IAdapterInterface
-from voltha.protos.adapter_pb2 import Adapter
-from voltha.protos.adapter_pb2 import AdapterConfig
-from voltha.protos.common_pb2 import LogLevel
-
-log = structlog.get_logger()
-
-@implementer(IAdapterInterface)
-class Asfvolt16Adapter(object):
-    name = 'asfvolt16_olt'
-
-    supported_device_types = [
-        DeviceType(
-            id=name,
-            adapter=name,
-            accepts_bulk_flow_update=False
-        )
-    ]
-
+class Asfvolt16Adapter(IAdapter):
     def __init__(self, adapter_agent, config):
-        self.adapter_agent = adapter_agent
-        self.config = config
-        self.descriptor = Adapter(
-            id=self.name,
-            vendor='Edgecore',
-            version='0.1',
-            config=AdapterConfig(log_level=LogLevel.INFO)
-        )
-
+        super(Asfvolt16Adapter, self).__init__(adapter_agent=adapter_agent,
+                                               config=config,
+                                               name='asfvolt16_olt',
+                                               vendor='Edgecore',
+                                               version='0.1')
         # register for adapter messages
         self.adapter_agent.register_for_inter_adapter_messages()
-
-    def start(self):
-        log.debug('starting')
-        log.info('started')
-
-    def stop(self):
-        log.debug('stopping')
-        log.info('stopped')
-
-    def adapter_descriptor(self):
-        return self.descriptor
-
-    def device_types(self):
-        return DeviceTypes(items=self.supported_device_types)
-
-    def health(self):
-        raise NotImplementedError()
-
-    def change_master_state(self, master):
-        raise NotImplementedError()
-
-    def update_pm_config(self, device, pm_config):
-        raise NotImplementedError()
-
-    def adopt_device(self, device):
-        raise NotImplementedError()
-
-    def reconcile_device(self, device):
-        raise NotImplementedError()
-
-    def abandon_device(self, device):
-        raise NotImplementedError()
-
-    def disable_device(self, device):
-        raise NotImplementedError()
-
-    def reenable_device(self, device):
-        raise NotImplementedError()
-
-    def reboot_device(self, device):
-        raise NotImplementedError()
-
-    def delete_device(self, device):
-        raise NotImplementedError()
-
-    def get_device_details(self, device):
-        raise NotImplementedError()
-
-    def update_flows_bulk(self, device, flows, groups):
-        raise NotImplementedError()
-
-    def update_flows_incrementally(self, device, flow_changes, group_changes):
-        raise NotImplementedError()
-
-    def send_proxied_message(self, proxy_address, msg):
-        raise NotImplementedError()
-
-    def receive_proxied_message(self, proxy_address, msg):
-        raise NotImplementedError()
-
-    def receive_packet_out(self, logical_device_id, egress_port_no, msg):
-        raise NotImplementedError()
-
-    def receive_inter_adapter_message(self, msg):
-        raise NotImplementedError()
-
-    def suppress_alarm(self, filter):
-        raise NotImplementedError()
-
-    def unsuppress_alarm(self, filter):
-        raise NotImplementedError()
diff --git a/voltha/adapters/iadapter.py b/voltha/adapters/iadapter.py
new file mode 100644
index 0000000..b94c35a
--- /dev/null
+++ b/voltha/adapters/iadapter.py
@@ -0,0 +1,121 @@
+#
+# 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.
+#
+
+"""
+Adapter abstract base class
+"""
+
+import structlog
+from zope.interface import implementer
+
+from voltha.protos.device_pb2 import DeviceType, DeviceTypes
+from voltha.adapters.interface import IAdapterInterface
+from voltha.protos.adapter_pb2 import Adapter
+from voltha.protos.adapter_pb2 import AdapterConfig
+from voltha.protos.common_pb2 import LogLevel
+from voltha.protos.health_pb2 import HealthStatus
+
+log = structlog.get_logger()
+
+@implementer(IAdapterInterface)
+class IAdapter(object):
+    def __init__(self, adapter_agent, config, name, vendor, version):
+        log.debug('Initializing adapter: {} {} {}'.format(vendor, name, version))
+        self.adapter_agent = adapter_agent
+        self.config = config
+        self.name = name
+        self.supported_device_types = [
+            DeviceType(
+                id=name,
+                adapter=name,
+                accepts_bulk_flow_update=True
+            )
+        ]
+        self.descriptor = Adapter(
+            id=self.name,
+            vendor=vendor,
+            version=version,
+            config=AdapterConfig(log_level=LogLevel.INFO)
+        )
+
+    def start(self):
+        log.info('Starting adapter: {}'.format(self.name))
+
+    def stop(self):
+        log.info('Stopping adapter: {}'.format(self.name))
+
+    def adapter_descriptor(self):
+        return self.descriptor
+
+    def device_types(self):
+        return DeviceTypes(items=self.supported_device_types)
+
+    def health(self):
+        return HealthStatus(state=HealthStatus.HealthState.HEALTHY)
+
+    def change_master_state(self, master):
+        raise NotImplementedError()
+
+    def update_pm_config(self, device, pm_config):
+        raise NotImplementedError()
+
+    def adopt_device(self, device):
+        raise NotImplementedError()
+
+    def reconcile_device(self, device):
+        raise NotImplementedError()
+
+    def abandon_device(self, device):
+        raise NotImplementedError()
+
+    def disable_device(self, device):
+        raise NotImplementedError()
+
+    def reenable_device(self, device):
+        raise NotImplementedError()
+
+    def reboot_device(self, device):
+        raise NotImplementedError()
+
+    def delete_device(self, device):
+        raise NotImplementedError()
+
+    def get_device_details(self, device):
+        raise NotImplementedError()
+
+    def update_flows_bulk(self, device, flows, groups):
+        raise NotImplementedError()
+
+    def update_flows_incrementally(self, device, flow_changes, group_changes):
+        raise NotImplementedError()
+
+    def send_proxied_message(self, proxy_address, msg):
+        raise NotImplementedError()
+
+    def receive_proxied_message(self, proxy_address, msg):
+        raise NotImplementedError()
+
+    def receive_packet_out(self, logical_device_id, egress_port_no, msg):
+        raise NotImplementedError()
+
+    def receive_inter_adapter_message(self, msg):
+        raise NotImplementedError()
+
+    def suppress_alarm(self, filter):
+        raise NotImplementedError()
+
+    def unsuppress_alarm(self, filter):
+        raise NotImplementedError()
diff --git a/voltha/adapters/ponsim_olt/ponsim_olt.py b/voltha/adapters/ponsim_olt/ponsim_olt.py
index be57fac..e6f42ab 100644
--- a/voltha/adapters/ponsim_olt/ponsim_olt.py
+++ b/voltha/adapters/ponsim_olt/ponsim_olt.py
@@ -27,23 +27,17 @@
 from scapy.layers.inet import Raw
 from twisted.internet import reactor
 from twisted.internet.defer import inlineCallbacks
-from zope.interface import implementer
 
 from common.frameio.frameio import BpfProgramFilter, hexify
 from common.utils.asleep import asleep
 from twisted.internet.task import LoopingCall
-from voltha.adapters.interface import IAdapterInterface
+from voltha.adapters.iadapter import IAdapter
 from voltha.core.logical_device_agent import mac_str_to_tuple
 from voltha.protos import third_party
 from voltha.protos import ponsim_pb2
-from voltha.protos.adapter_pb2 import Adapter
-from voltha.protos.adapter_pb2 import AdapterConfig
-from voltha.protos.common_pb2 import LogLevel, OperStatus, ConnectStatus, \
-    AdminState
-from voltha.protos.device_pb2 import DeviceType, DeviceTypes, Port, Device, \
-    PmConfig, PmConfigs
+from voltha.protos.common_pb2 import OperStatus, ConnectStatus, AdminState
+from voltha.protos.device_pb2 import Port, Device, PmConfig, PmConfigs
 from voltha.protos.events_pb2 import KpiEvent, KpiEventType, MetricValuePairs
-from voltha.protos.health_pb2 import HealthStatus
 from google.protobuf.empty_pb2 import Empty
 
 from voltha.protos.logical_device_pb2 import LogicalPort, LogicalDevice
@@ -181,56 +175,16 @@
         except Exception as e:
             log.exception('failed-to-send-alarm', e=e)
 
-
-@implementer(IAdapterInterface)
-class PonSimOltAdapter(object):
-    name = 'ponsim_olt'
-
-    supported_device_types = [
-        DeviceType(
-            id=name,
-            adapter=name,
-            accepts_bulk_flow_update=True
-        )
-    ]
-
+class PonSimOltAdapter(IAdapter):
     def __init__(self, adapter_agent, config):
-        self.adapter_agent = adapter_agent
-        self.config = config
-        self.descriptor = Adapter(
-            id=self.name,
-            vendor='Voltha project',
-            version='0.4',
-            config=AdapterConfig(log_level=LogLevel.INFO)
-        )
+        super(PonSimOltAdapter, self).__init__(adapter_agent=adapter_agent,
+                                               config=config,
+                                               name='ponsim_olt',
+                                               vendor='Voltha project',
+                                               version='0.4')
         self.devices_handlers = dict()  # device_id -> PonSimOltHandler()
         self.logical_device_id_to_root_device_id = dict()
 
-    def start(self):
-        log.debug('starting')
-        log.info('started')
-
-    def stop(self):
-        """
-        This method is called when this device instance is no longer
-        required,  which means there is a request to remove this device.
-        :return:
-        """
-        log.debug('stopping')
-        log.info('stopped')
-
-    def adapter_descriptor(self):
-        return self.descriptor
-
-    def device_types(self):
-        return DeviceTypes(items=self.supported_device_types)
-
-    def health(self):
-        return HealthStatus(state=HealthStatus.HealthState.HEALTHY)
-
-    def change_master_state(self, master):
-        raise NotImplementedError()
-
     def update_pm_config(self, device, pm_config):
         log.info("adapter-update-pm-config", device=device,
                  pm_config=pm_config)
@@ -259,9 +213,6 @@
         except Exception, e:
             log.exception('Exception', e=e)
 
-    def abandon_device(self, device):
-        raise NotImplementedError()
-
     def disable_device(self, device):
         log.info('disable-device', device_id=device.id)
         reactor.callLater(0, self.devices_handlers[device.id].disable)
@@ -283,9 +234,6 @@
         reactor.callLater(0, self.devices_handlers[device.id].delete)
         return device
 
-    def get_device_details(self, device):
-        raise NotImplementedError()
-
     def update_flows_bulk(self, device, flows, groups):
         log.info('bulk-flow-update', device_id=device.id,
                  flows=flows, groups=groups)
@@ -293,17 +241,11 @@
         handler = self.devices_handlers[device.id]
         return handler.update_flow_table(flows.items)
 
-    def update_flows_incrementally(self, device, flow_changes, group_changes):
-        raise NotImplementedError()
-
     def send_proxied_message(self, proxy_address, msg):
         log.info('send-proxied-message', proxy_address=proxy_address, msg=msg)
         handler = self.devices_handlers[proxy_address.device_id]
         handler.send_proxied_message(proxy_address, msg)
 
-    def receive_proxied_message(self, proxy_address, msg):
-        raise NotImplementedError()
-
     def receive_packet_out(self, logical_device_id, egress_port_no, msg):
         def ldi_to_di(ldi):
             di = self.logical_device_id_to_root_device_id.get(ldi)
@@ -317,16 +259,6 @@
         handler = self.devices_handlers[device_id]
         handler.packet_out(egress_port_no, msg)
 
-    def receive_inter_adapter_message(self, msg):
-        raise NotImplementedError()
-
-    def suppress_alarm(self, filter):
-        raise NotImplementedError()
-
-    def unsuppress_alarm(self, filter):
-        raise NotImplementedError()
-
-
 class PonSimOltHandler(object):
     def __init__(self, adapter, device_id):
         self.adapter = adapter
diff --git a/voltha/adapters/ponsim_onu/ponsim_onu.py b/voltha/adapters/ponsim_onu/ponsim_onu.py
index 05df340..781e212 100644
--- a/voltha/adapters/ponsim_onu/ponsim_onu.py
+++ b/voltha/adapters/ponsim_onu/ponsim_onu.py
@@ -21,21 +21,16 @@
 import structlog
 from twisted.internet import reactor
 from twisted.internet.defer import DeferredQueue, inlineCallbacks
-from zope.interface import implementer
 from common.utils.asleep import asleep
 
-from voltha.adapters.interface import IAdapterInterface
+from voltha.adapters.iadapter import IAdapter
 from voltha.core.logical_device_agent import mac_str_to_tuple
 from voltha.protos import third_party
-from voltha.protos.adapter_pb2 import Adapter
-from voltha.protos.adapter_pb2 import AdapterConfig
-from voltha.protos.common_pb2 import LogLevel, OperStatus, ConnectStatus, \
-    AdminState
-from voltha.protos.device_pb2 import DeviceType, DeviceTypes, Port
-from voltha.protos.health_pb2 import HealthStatus
+from voltha.protos.common_pb2 import OperStatus, ConnectStatus, AdminState
+from voltha.protos.device_pb2 import Port
 from voltha.protos.logical_device_pb2 import LogicalPort
 from voltha.protos.openflow_13_pb2 import OFPPS_LIVE, OFPPF_FIBER, \
-    OFPPF_1GB_FD, OFPPC_NO_RECV
+    OFPPF_1GB_FD
 from voltha.protos.openflow_13_pb2 import ofp_port
 from voltha.protos.ponsim_pb2 import FlowTable
 
@@ -43,52 +38,15 @@
 log = structlog.get_logger()
 
 
-@implementer(IAdapterInterface)
-class PonSimOnuAdapter(object):
-    name = 'ponsim_onu'
-
-    supported_device_types = [
-        DeviceType(
-            id=name,
-            adapter=name,
-            accepts_bulk_flow_update=True
-        )
-    ]
-
+class PonSimOnuAdapter(IAdapter):
     def __init__(self, adapter_agent, config):
-        self.adapter_agent = adapter_agent
-        self.config = config
-        self.descriptor = Adapter(
-            id=self.name,
-            vendor='Voltha project',
-            version='0.4',
-            config=AdapterConfig(log_level=LogLevel.INFO)
-        )
+        super(PonSimOnuAdapter, self).__init__(adapter_agent=adapter_agent,
+                                               config=config,
+                                               name='ponsim_onu',
+                                               vendor='Voltha project',
+                                               version='0.4')
         self.devices_handlers = dict()  # device_id -> PonSimOltHandler()
 
-    def start(self):
-        log.debug('starting')
-        log.info('started')
-
-    def stop(self):
-        log.debug('stopping')
-        log.info('stopped')
-
-    def adapter_descriptor(self):
-        return self.descriptor
-
-    def device_types(self):
-        return DeviceTypes(items=self.supported_device_types)
-
-    def health(self):
-        return HealthStatus(state=HealthStatus.HealthState.HEALTHY)
-
-    def change_master_state(self, master):
-        raise NotImplementedError()
-
-    def update_pm_config(self, device, pm_configs):
-        raise NotImplementedError()
-
     def adopt_device(self, device):
         self.devices_handlers[device.id] = PonSimOnuHandler(self, device.id)
         reactor.callLater(0, self.devices_handlers[device.id].activate, device)
@@ -103,9 +61,6 @@
                               device)
         return device
 
-    def abandon_device(self, device):
-        raise NotImplementedError()
-
     def disable_device(self, device):
         log.info('disable-device', device_id=device.id)
         reactor.callLater(0, self.devices_handlers[device.id].disable)
@@ -136,9 +91,6 @@
         handler = self.devices_handlers[device.id]
         return handler.update_flow_table(flows.items)
 
-    def update_flows_incrementally(self, device, flow_changes, group_changes):
-        raise NotImplementedError()
-
     def send_proxied_message(self, proxy_address, msg):
         log.info('send-proxied-message', proxy_address=proxy_address, msg=msg)
 
@@ -157,16 +109,6 @@
         log.info('packet-out', logical_device_id=logical_device_id,
                  egress_port_no=egress_port_no, msg_len=len(msg))
 
-    def receive_inter_adapter_message(self, msg):
-        raise NotImplementedError()
-
-    def suppress_alarm(self, filter):
-        raise NotImplementedError()
-
-    def unsuppress_alarm(self, filter):
-        raise NotImplementedError()
-
-
 class PonSimOnuHandler(object):
     def __init__(self, adapter, device_id):
         self.adapter = adapter