VOL-1448: Initial checkin of pyvoltha repository
This is very early work and unit tests are not currently running.
Future versions of this code will remove the protobuf directory
and address any v2.0 API changes such as the key-value store API
used by various libraries in pyvoltha
- Added .gitreview config file
- Moved VERSION file to expected location and specified a dev version
  so no git tags or PyPI publishing occurs until we are ready.
- Removed generated .desc protobuf files
Change-Id: Icaedc6a4d2cff87cd7d538d3610586d0f5a5db18
diff --git a/test/unit/common/__init__.py b/test/unit/common/__init__.py
new file mode 100644
index 0000000..b0fb0b2
--- /dev/null
+++ b/test/unit/common/__init__.py
@@ -0,0 +1,13 @@
+# Copyright 2017-present Open Networking Foundation
+#
+# 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.
diff --git a/test/unit/common/test_event_bus.py b/test/unit/common/test_event_bus.py
new file mode 100644
index 0000000..be325e8
--- /dev/null
+++ b/test/unit/common/test_event_bus.py
@@ -0,0 +1,220 @@
+#
+# Copyright 2016 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 re
+
+from mock import Mock
+from mock import call
+from twisted.internet.defer import DeferredQueue, inlineCallbacks
+from twisted.trial.unittest import TestCase
+
+from pyvoltha.common.event_bus import EventBusClient, EventBus
+
+
+class TestEventBus(TestCase):
+
+    def test_subscribe(self):
+
+        ebc = EventBusClient()
+        sub = ebc.subscribe('news', lambda msg, topic: None)
+        self.assertEqual(len(ebc.list_subscribers()), 1)
+        self.assertEqual(len(ebc.list_subscribers('news')), 1)
+        self.assertEqual(len(ebc.list_subscribers('other')), 0)
+
+    def test_unsubscribe(self):
+
+        ebc = EventBusClient(EventBus())
+        sub = ebc.subscribe('news', lambda msg, topic: None)
+        ebc.unsubscribe(sub)
+        self.assertEqual(ebc.list_subscribers(), [])
+        self.assertEqual(ebc.list_subscribers('news'), [])
+
+    def test_simple_publish(self):
+
+        ebc = EventBusClient(EventBus())
+
+        mock = Mock()
+        ebc.subscribe('news', mock)
+
+        ebc.publish('news', 'message')
+
+        self.assertEqual(mock.call_count, 1)
+        mock.assert_called_with('news', 'message')
+
+    def test_topic_filtering(self):
+
+        ebc = EventBusClient(EventBus())
+
+        mock = Mock()
+        ebc.subscribe('news', mock)
+
+        ebc.publish('news', 'msg1')
+        ebc.publish('alerts', 'msg2')
+        ebc.publish('logs', 'msg3')
+
+        self.assertEqual(mock.call_count, 1)
+        mock.assert_called_with('news', 'msg1')
+
+    def test_multiple_subscribers(self):
+
+        ebc = EventBusClient(EventBus())
+
+        mock1 = Mock()
+        ebc.subscribe('news', mock1)
+
+        mock2 = Mock()
+        ebc.subscribe('alerts', mock2)
+
+        mock3 = Mock()
+        ebc.subscribe('logs', mock3)
+
+        mock4 = Mock()
+        ebc.subscribe('logs', mock4)
+
+        ebc.publish('news', 'msg1')
+        ebc.publish('alerts', 'msg2')
+        ebc.publish('logs', 'msg3')
+
+        self.assertEqual(mock1.call_count, 1)
+        mock1.assert_called_with('news', 'msg1')
+
+        self.assertEqual(mock2.call_count, 1)
+        mock2.assert_called_with('alerts', 'msg2')
+
+        self.assertEqual(mock3.call_count, 1)
+        mock3.assert_called_with('logs', 'msg3')
+
+        self.assertEqual(mock4.call_count, 1)
+        mock4.assert_called_with('logs', 'msg3')
+
+    def test_predicates(self):
+
+        ebc = EventBusClient(EventBus())
+
+        get_foos = Mock()
+        ebc.subscribe('', get_foos, lambda msg: msg.startswith('foo'))
+
+        get_bars = Mock()
+        ebc.subscribe('', get_bars, lambda msg: msg.endswith('bar'))
+
+        get_all = Mock()
+        ebc.subscribe('', get_all)
+
+        get_none = Mock()
+        ebc.subscribe('', get_none, lambda msg: msg.find('zoo') >= 0)
+
+        errored = Mock()
+        ebc.subscribe('', errored, lambda msg: 1/0)
+
+        ebc.publish('', 'foo')
+        ebc.publish('', 'foobar')
+        ebc.publish('', 'bar')
+
+        c = call
+
+        self.assertEqual(get_foos.call_count, 2)
+        get_foos.assert_has_calls([c('', 'foo'), c('', 'foobar')])
+
+        self.assertEqual(get_bars.call_count, 2)
+        get_bars.assert_has_calls([c('', 'foobar'), c('', 'bar')])
+
+        self.assertEqual(get_all.call_count, 3)
+        get_all.assert_has_calls([c('', 'foo'), c('', 'foobar'), c('', 'bar')])
+
+        get_none.assert_not_called()
+
+        errored.assert_not_called()
+
+    def test_wildcard_topic(self):
+
+        ebc = EventBusClient(EventBus())
+        subs = []
+
+        wildcard_sub = Mock()
+        subs.append(ebc.subscribe(re.compile(r'.*'), wildcard_sub))
+
+        prefix_sub = Mock()
+        subs.append(ebc.subscribe(re.compile(r'ham.*'), prefix_sub))
+
+        contains_sub = Mock()
+        subs.append(ebc.subscribe(re.compile(r'.*burg.*'), contains_sub))
+
+        ebc.publish('news', 1)
+        ebc.publish('hamsters', 2)
+        ebc.publish('hamburgers', 3)
+        ebc.publish('nonsense', 4)
+
+        c = call
+
+        self.assertEqual(wildcard_sub.call_count, 4)
+        wildcard_sub.assert_has_calls([
+            c('news', 1),
+            c('hamsters', 2),
+            c('hamburgers', 3),
+            c('nonsense', 4)])
+
+        self.assertEqual(prefix_sub.call_count, 2)
+        prefix_sub.assert_has_calls([
+            c('hamsters', 2),
+            c('hamburgers', 3)])
+
+        self.assertEqual(contains_sub.call_count, 1)
+        contains_sub.assert_has_calls([c('hamburgers', 3)])
+
+        for sub in subs:
+            ebc.unsubscribe(sub)
+
+        self.assertEqual(ebc.list_subscribers(), [])
+
+    @inlineCallbacks
+    def test_deferred_queue_receiver(self):
+
+        ebc = EventBus()
+
+        queue = DeferredQueue()
+
+        ebc.subscribe('', lambda _, msg: queue.put(msg))
+
+        for i in xrange(10):
+            ebc.publish('', i)
+
+        self.assertEqual(len(queue.pending), 10)
+        for i in xrange(10):
+            msg = yield queue.get()
+            self.assertEqual(msg, i)
+        self.assertEqual(len(queue.pending), 0)
+
+    def test_subscribers_that_unsubscribe_when_called(self):
+        # VOL-943 bug fix check
+        ebc = EventBusClient(EventBus())
+
+        class UnsubscribeWhenCalled(object):
+            def __init__(self):
+                self.subscription = ebc.subscribe('news', self.unsubscribe)
+                self.called = False
+
+            def unsubscribe(self, _topic, _msg):
+                self.called = True
+                ebc.unsubscribe(self.subscription)
+
+        ebc1 = UnsubscribeWhenCalled()
+        ebc2 = UnsubscribeWhenCalled()
+        ebc3 = UnsubscribeWhenCalled()
+
+        ebc.publish('news', 'msg1')
+
+        self.assertTrue(ebc1.called)
+        self.assertTrue(ebc2.called)
+        self.assertTrue(ebc3.called)
diff --git a/test/unit/common/utils/__init__.py b/test/unit/common/utils/__init__.py
new file mode 100644
index 0000000..b0fb0b2
--- /dev/null
+++ b/test/unit/common/utils/__init__.py
@@ -0,0 +1,13 @@
+# Copyright 2017-present Open Networking Foundation
+#
+# 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.
diff --git a/test/unit/common/utils/test_bpf.py b/test/unit/common/utils/test_bpf.py
new file mode 100644
index 0000000..21a80da
--- /dev/null
+++ b/test/unit/common/utils/test_bpf.py
@@ -0,0 +1,48 @@
+# Copyright 2017-present Open Networking Foundation
+#
+# 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 unittest import TestCase, main
+
+from scapy.layers.l2 import Ether, Dot1Q
+
+from pyvoltha.adapters.common.frameio.frameio import BpfProgramFilter
+
+
+class TestBpf(TestCase):
+
+    def test_bpf1(self):
+        vid = 4090
+        pcp = 7
+        frame_match = 'ether[14:2] = 0x{:01x}{:03x}'.format(pcp << 1, vid)
+        filter = BpfProgramFilter(frame_match)
+        self.assertTrue(filter(str(Ether()/Dot1Q(prio=pcp, vlan=vid))))
+        self.assertFalse(filter(str(Ether()/Dot1Q(prio=pcp, vlan=4000))))
+
+    def test_bpf2(self):
+        vid1 = 4090
+        pcp1 = 7
+        frame_match_case1 = 'ether[14:2] = 0x{:01x}{:03x}'.format(
+            pcp1 << 1, vid1)
+
+        vid2 = 4000
+        frame_match_case2 = '(ether[14:2] & 0xfff) = 0x{:03x}'.format(vid2)
+
+        filter = BpfProgramFilter('{} or {}'.format(
+            frame_match_case1, frame_match_case2))
+        self.assertTrue(filter(str(Ether()/Dot1Q(prio=pcp1, vlan=vid1))))
+        self.assertTrue(filter(str(Ether()/Dot1Q(vlan=vid2))))
+        self.assertFalse(filter(str(Ether()/Dot1Q(vlan=4001))))
+
+
+if __name__ == '__main__':
+    main()
diff --git a/test/unit/common/utils/test_indexpool.py b/test/unit/common/utils/test_indexpool.py
new file mode 100644
index 0000000..7eb1050
--- /dev/null
+++ b/test/unit/common/utils/test_indexpool.py
@@ -0,0 +1,48 @@
+# Copyright 2017-present Open Networking Foundation
+#
+# 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 unittest import TestCase, main
+from pyvoltha.common.utils.indexpool import IndexPool
+
+class TestIndexPool(TestCase):
+    pool = IndexPool(8, 0)
+    def test_01_get_next(self):
+        self.assertEqual(self.pool.indices.bin, '00000000')
+        for i in range(8):
+            self.assertEqual(self.pool.get_next(), i)
+        #to check if there's any bit left after using all 8 bits
+        self.assertIsNone(self.pool.get_next())
+
+    def test_02_pre_allocate(self):
+        _pool2 = IndexPool(8, 0)
+        self.assertEqual(_pool2.indices.bin, '00000000')
+        _pool2.pre_allocate((0,1,2,))
+        self.assertEqual(_pool2.indices.bin, '11100000')
+
+    def test_03_release(self):
+        self.pool.release(5)
+        self.assertEqual(self.pool.indices.bin, '11111011')
+        self.pool.release(10)
+        self.assertEqual(self.pool.indices.bin, '11111011')
+        self.pool.release(0)
+        self.assertEqual(self.pool.indices.bin, '01111011')
+
+    def test_04_check_offset(self):
+        _offset = 5
+        self.pool = IndexPool(8, _offset)
+        for i in range(8):
+            self.assertEqual(self.pool.get_next(), _offset + i)
+
+
+if __name__ == '__main__':
+    main()
\ No newline at end of file
diff --git a/test/unit/common/utils/test_ordered_weakvalue_dict.py b/test/unit/common/utils/test_ordered_weakvalue_dict.py
new file mode 100644
index 0000000..a8ce47c
--- /dev/null
+++ b/test/unit/common/utils/test_ordered_weakvalue_dict.py
@@ -0,0 +1,51 @@
+# Copyright 2017-present Open Networking Foundation
+#
+# 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 unittest import TestCase, main
+
+from pyvoltha.common.utils.ordered_weakvalue_dict import OrderedWeakValueDict
+
+
+class O(object):
+    def __init__(self, a):
+        self.a = a
+
+
+class TestOrderedWeakValueDict(TestCase):
+
+    def test_standard_behavior(self):
+        holder = dict()  # a real dict so we can control which object real ref
+        def mk(k):
+            o = O(k)
+            holder[k] = o
+            return o
+        o = OrderedWeakValueDict((k, mk(k)) for k in xrange(10))
+        self.assertEqual(len(o), 10)
+        self.assertEqual(o[3].a, 3)
+        o[3] = mk(-3)
+        self.assertEqual(o[3].a, -3)
+        del o[3]
+        self.assertEqual(len(o), 9)
+        o[100] = mk(100)
+        self.assertEqual(len(o), 10)
+        self.assertEqual(o.keys(), [0, 1, 2, 4, 5, 6, 7, 8, 9, 100])
+
+        # remove a few items from the holder, they should be gone from o too
+        del holder[1]
+        del holder[5]
+        del holder[6]
+
+        self.assertEqual(o.keys(), [0, 2, 4, 7, 8, 9, 100])
+        self.assertEqual([v.a for v in o.values()], [0, 2, 4, 7, 8, 9, 100])
+
+