blob: 6205ee49c140ec4d3f077bb83d52181ea18bb395 [file] [log] [blame]
Girish Gowdru141ced82018-09-17 20:19:14 -07001#
2# Copyright 2018 the original author or authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16import json
17from unittest import TestCase, main
18
19from bitstring import BitArray
Girish Gowdru141ced82018-09-17 20:19:14 -070020from common.pon_resource_manager.resource_manager import PONResourceManager
21from mock import Mock
Girish Gowdru141ced82018-09-17 20:19:14 -070022
23
24class TestResourceManager(TestCase):
25 def setUp(self):
26 self._rm = PONResourceManager('xgspon', 'default',
27 '0001c889ee7189fb', 'consul',
28 'localhost', 8500)
29 self.default_resource_range = {
Girish Gowdru1e77ea02018-09-24 09:10:35 -070030 "onu_id_start": 1,
31 "onu_id_end": 127,
32 "alloc_id_start": 1024,
33 "alloc_id_end": 2816,
34 "gemport_id_start": 1024,
35 "gemport_id_end": 8960,
36 "pon_ports": 16
Girish Gowdru141ced82018-09-17 20:19:14 -070037 }
38
39 def tearDown(self):
40 self._rm = None
41 self.default_resource_range = None
42
Girish Gowdru141ced82018-09-17 20:19:14 -070043 def test_init_pon_resource_ranges(self):
Girish Gowdru1e77ea02018-09-24 09:10:35 -070044 output = json.dumps(self.default_resource_range).encode('utf-8')
45 self._rm._get_olt_vendor = Mock(return_value='default')
46 self._rm._kv_store.get_from_kv_store = Mock(return_value=output)
Girish Gowdru141ced82018-09-17 20:19:14 -070047
Girish Gowdru1e77ea02018-09-24 09:10:35 -070048 self._rm.init_resource_ranges_from_kv_store()
Girish Gowdru141ced82018-09-17 20:19:14 -070049 self.assertEqual(self._rm.pon_resource_ranges,
50 self.default_resource_range)
51
Girish Gowdru1e77ea02018-09-24 09:10:35 -070052 self._rm.init_default_pon_resource_ranges()
Girish Gowdru141ced82018-09-17 20:19:14 -070053 self.assertEqual(self._rm.pon_resource_ranges,
54 self.default_resource_range)
55
Girish Gowdru141ced82018-09-17 20:19:14 -070056 def test_init_resource_id_pool(self):
Girish Gowdru1e77ea02018-09-24 09:10:35 -070057 self._rm._kv_store.get_from_kv_store = Mock(return_value=None)
58 self._rm._kv_store.update_to_kv_store = Mock(return_value=True)
59 status = self._rm.init_resource_id_pool(0, 'ONU_ID', 1, 127)
Girish Gowdru141ced82018-09-17 20:19:14 -070060 self.assertTrue(status)
Girish Gowdru1e77ea02018-09-24 09:10:35 -070061 status = self._rm.init_resource_id_pool(
Girish Gowdru141ced82018-09-17 20:19:14 -070062 1, 'ALLOC_ID', 1024, 16383)
63 self.assertTrue(status)
Girish Gowdru1e77ea02018-09-24 09:10:35 -070064 status = self._rm.init_resource_id_pool(
Girish Gowdru141ced82018-09-17 20:19:14 -070065 2, 'GEMPORT_ID', 1023, 65534)
66 self.assertTrue(status)
67
Girish Gowdru141ced82018-09-17 20:19:14 -070068 def test_get_resource_id(self):
69 # Get onu id test
70 onu_id_resource = self._rm._format_resource(0, 1, 127)
Girish Gowdru1e77ea02018-09-24 09:10:35 -070071 output = onu_id_resource.encode('utf-8')
72 self._rm._kv_store.get_from_kv_store = Mock(return_value=output)
73 self._rm._kv_store.update_to_kv_store = Mock(return_value=True)
74 result = self._rm.get_resource_id(0, 'ONU_ID')
Girish Gowdru141ced82018-09-17 20:19:14 -070075 self.assertEqual(result, 1)
76
77 # Get alloc id test
78 alloc_id_resource = self._rm._format_resource(1, 1024, 16383)
Girish Gowdru1e77ea02018-09-24 09:10:35 -070079 output = alloc_id_resource.encode('utf-8')
80 self._rm._kv_store.get_from_kv_store = Mock(return_value=output)
81 result = self._rm.get_resource_id(1, 'ALLOC_ID', 1)
Girish Gowdru141ced82018-09-17 20:19:14 -070082 self.assertEqual(result[0], 1024)
Girish Gowdru1e77ea02018-09-24 09:10:35 -070083 result = self._rm.get_resource_id(1, 'ALLOC_ID', 4)
Girish Gowdru141ced82018-09-17 20:19:14 -070084 self.assertEqual(result, [1024, 1025, 1026, 1027])
85
86 # Get gemport id test
87 gemport_id_resource = self._rm._format_resource(2, 1023, 65534)
Girish Gowdru1e77ea02018-09-24 09:10:35 -070088 output = gemport_id_resource.encode('utf-8')
89 self._rm._kv_store.get_from_kv_store = Mock(return_value=output)
90 result = self._rm.get_resource_id(2, 'GEMPORT_ID', 1)
Girish Gowdru141ced82018-09-17 20:19:14 -070091 self.assertEqual(result[0], 1023)
Girish Gowdru1e77ea02018-09-24 09:10:35 -070092 result = self._rm.get_resource_id(2, 'GEMPORT_ID', 5)
Girish Gowdru141ced82018-09-17 20:19:14 -070093 self.assertEqual(result, [1023, 1024, 1025, 1026, 1027])
94
Girish Gowdru141ced82018-09-17 20:19:14 -070095 def test_free_resource_id(self):
96 # Free onu id test
Girish Gowdru1e77ea02018-09-24 09:10:35 -070097 self._rm._kv_store.update_to_kv_store = Mock(return_value=True)
Girish Gowdru141ced82018-09-17 20:19:14 -070098 onu_id_resource = eval(self._rm._format_resource(0, 1, 127))
99 onu_id_resource['pool'] = BitArray('0b' + onu_id_resource['pool'])
100 self._rm._generate_next_id(onu_id_resource)
101 onu_id_resource['pool'] = onu_id_resource['pool'].bin
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700102 output = json.dumps(onu_id_resource).encode('utf-8')
103 self._rm._kv_store.get_from_kv_store = Mock(return_value=output)
104 result = self._rm.free_resource_id(0, 'ONU_ID', 1)
Girish Gowdru141ced82018-09-17 20:19:14 -0700105 self.assertTrue(result)
106
107 # Free alloc id test
108 alloc_id_resource = eval(self._rm._format_resource(1, 1024, 16383))
109 alloc_id_resource['pool'] = BitArray('0b' + alloc_id_resource['pool'])
110
111 for num in range(5):
112 self._rm._generate_next_id(alloc_id_resource)
113
114 alloc_id_resource['pool'] = alloc_id_resource['pool'].bin
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700115 output = json.dumps(alloc_id_resource).encode('utf-8')
116 self._rm._kv_store.get_from_kv_store = Mock(return_value=output)
Girish Gowdru141ced82018-09-17 20:19:14 -0700117 result = self._rm.free_resource_id(1, 'ALLOC_ID',
118 [1024, 1025, 1026, 1027, 1028])
119 self.assertTrue(result)
120
121 # Free gemport id test
122 gemport_id_resource = eval(self._rm._format_resource(2, 1023, 65534))
123 gemport_id_resource['pool'] = BitArray(
124 '0b' + gemport_id_resource['pool'])
125
126 for num in range(6):
127 self._rm._generate_next_id(gemport_id_resource)
128
129 gemport_id_resource['pool'] = gemport_id_resource['pool'].bin
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700130 output = json.dumps(gemport_id_resource).encode('utf-8')
131 self._rm._kv_store.get_from_kv_store = Mock(return_value=output)
Girish Gowdru141ced82018-09-17 20:19:14 -0700132 result = self._rm.free_resource_id(2, 'GEMPORT_ID',
133 [1023, 1024, 1025, 1026, 1027, 1028])
134 self.assertTrue(result)
135
Girish Gowdru141ced82018-09-17 20:19:14 -0700136 def test_clear_resource_id_pool(self):
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700137 self._rm._kv_store.remove_from_kv_store = Mock(return_value=True)
138 status = self._rm.clear_resource_id_pool(0, 'ONU_ID')
Girish Gowdru141ced82018-09-17 20:19:14 -0700139 self.assertTrue(status)
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700140 self._rm._kv_store.remove_from_kv_store = Mock(return_value=False)
141 status = self._rm.clear_resource_id_pool(1, 'ALLOC_ID')
Girish Gowdru141ced82018-09-17 20:19:14 -0700142 self.assertFalse(status)
143
144
145if __name__ == '__main__':
146 main()