blob: 74c473672c1b5695d60a579fc1de91b7a9f8b5b5 [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
20from common.kvstore.kv_client import KVPair
21from common.pon_resource_manager.resource_manager import PONResourceManager
22from mock import Mock
23from twisted.internet.defer import inlineCallbacks
24
25
26class TestResourceManager(TestCase):
27 def setUp(self):
28 self._rm = PONResourceManager('xgspon', 'default',
29 '0001c889ee7189fb', 'consul',
30 'localhost', 8500)
31 self.default_resource_range = {
32 "onu_start_idx": 1,
33 "onu_end_idx": 127,
34 "alloc_id_start_idx": 1024,
35 "alloc_id_end_idx": 65534,
36 "gem_port_id_start_idx": 1024,
37 "gem_port_id_end_idx": 16383,
38 "num_of_pon_port": 16
39 }
40
41 def tearDown(self):
42 self._rm = None
43 self.default_resource_range = None
44
45 @inlineCallbacks
46 def test_init_pon_resource_ranges(self):
47 key = PONResourceManager.PON_RESOURCE_RANGE_CONFIG_PATH.format(
48 'xgspon', 'default')
49 value = json.dumps(self.default_resource_range).encode('utf-8')
50 output = KVPair(key, value, None)
51 self._rm._kv_store.get = Mock(return_value=(output, None))
52
53 yield self._rm.init_pon_resource_ranges()
54 self.assertEqual(self._rm.pon_resource_ranges,
55 self.default_resource_range)
56
57 self._rm._kv_store.get = Mock(return_value=(None, None))
58
59 yield self._rm.init_pon_resource_ranges()
60 self.assertEqual(self._rm.pon_resource_ranges,
61 self.default_resource_range)
62
63 @inlineCallbacks
64 def test_init_resource_id_pool(self):
65 self._rm._kv_store.get = Mock(return_value=(None, None))
66 self._rm._kv_store.put = Mock(return_value=None)
67 status = yield self._rm.init_resource_id_pool(0, 'ONU_ID', 1, 127)
68 self.assertTrue(status)
69 status = yield self._rm.init_resource_id_pool(
70 1, 'ALLOC_ID', 1024, 16383)
71 self.assertTrue(status)
72 status = yield self._rm.init_resource_id_pool(
73 2, 'GEMPORT_ID', 1023, 65534)
74 self.assertTrue(status)
75
76 @inlineCallbacks
77 def test_get_resource_id(self):
78 # Get onu id test
79 onu_id_resource = self._rm._format_resource(0, 1, 127)
80 key = self._rm._get_path(0, PONResourceManager.ONU_ID)
81 value = onu_id_resource.encode('utf-8')
82 output = KVPair(key, value, None)
83 self._rm._kv_store.get = Mock(return_value=(output, None))
84 self._rm._kv_store.put = Mock(return_value=None)
85 result = yield self._rm.get_resource_id(0, 'ONU_ID')
86 self.assertEqual(result, 1)
87
88 # Get alloc id test
89 alloc_id_resource = self._rm._format_resource(1, 1024, 16383)
90 key = self._rm._get_path(1, PONResourceManager.ALLOC_ID)
91 value = alloc_id_resource.encode('utf-8')
92 output = KVPair(key, value, None)
93 self._rm._kv_store.get = Mock(return_value=(output, None))
94 result = yield self._rm.get_resource_id(1, 'ALLOC_ID', 1)
95 self.assertEqual(result[0], 1024)
96 result = yield self._rm.get_resource_id(1, 'ALLOC_ID', 4)
97 self.assertEqual(result, [1024, 1025, 1026, 1027])
98
99 # Get gemport id test
100 gemport_id_resource = self._rm._format_resource(2, 1023, 65534)
101 key = self._rm._get_path(2, PONResourceManager.GEMPORT_ID)
102 value = gemport_id_resource.encode('utf-8')
103 output = KVPair(key, value, None)
104 self._rm._kv_store.get = Mock(return_value=(output, None))
105 result = yield self._rm.get_resource_id(2, 'GEMPORT_ID', 1)
106 self.assertEqual(result[0], 1023)
107 result = yield self._rm.get_resource_id(2, 'GEMPORT_ID', 5)
108 self.assertEqual(result, [1023, 1024, 1025, 1026, 1027])
109
110 @inlineCallbacks
111 def test_free_resource_id(self):
112 # Free onu id test
113 self._rm._kv_store.put = Mock(return_value=None)
114 onu_id_resource = eval(self._rm._format_resource(0, 1, 127))
115 onu_id_resource['pool'] = BitArray('0b' + onu_id_resource['pool'])
116 self._rm._generate_next_id(onu_id_resource)
117 onu_id_resource['pool'] = onu_id_resource['pool'].bin
118 key = self._rm._get_path(0, PONResourceManager.ONU_ID)
119 value = json.dumps(onu_id_resource).encode('utf-8')
120 output = KVPair(key, value, None)
121 self._rm._kv_store.get = Mock(return_value=(output, None))
122 result = yield self._rm.free_resource_id(0, 'ONU_ID', 1)
123 self.assertTrue(result)
124
125 # Free alloc id test
126 alloc_id_resource = eval(self._rm._format_resource(1, 1024, 16383))
127 alloc_id_resource['pool'] = BitArray('0b' + alloc_id_resource['pool'])
128
129 for num in range(5):
130 self._rm._generate_next_id(alloc_id_resource)
131
132 alloc_id_resource['pool'] = alloc_id_resource['pool'].bin
133 key = self._rm._get_path(0, PONResourceManager.ALLOC_ID)
134 value = json.dumps(alloc_id_resource).encode('utf-8')
135 output = KVPair(key, value, None)
136 self._rm._kv_store.get = Mock(return_value=(output, None))
137 result = self._rm.free_resource_id(1, 'ALLOC_ID',
138 [1024, 1025, 1026, 1027, 1028])
139 self.assertTrue(result)
140
141 # Free gemport id test
142 gemport_id_resource = eval(self._rm._format_resource(2, 1023, 65534))
143 gemport_id_resource['pool'] = BitArray(
144 '0b' + gemport_id_resource['pool'])
145
146 for num in range(6):
147 self._rm._generate_next_id(gemport_id_resource)
148
149 gemport_id_resource['pool'] = gemport_id_resource['pool'].bin
150 key = self._rm._get_path(0, PONResourceManager.GEMPORT_ID)
151 value = json.dumps(gemport_id_resource).encode('utf-8')
152 output = KVPair(key, value, None)
153 self._rm._kv_store.get = Mock(return_value=(output, None))
154 result = self._rm.free_resource_id(2, 'GEMPORT_ID',
155 [1023, 1024, 1025, 1026, 1027, 1028])
156 self.assertTrue(result)
157
158 @inlineCallbacks
159 def test_clear_resource_id_pool(self):
160 self._rm._kv_store.delete = Mock(return_value=None)
161 status = yield self._rm.clear_resource_id_pool(0, 'ONU_ID')
162 self.assertTrue(status)
163 self._rm._kv_store.delete = Mock(return_value="error")
164 status = yield self._rm.clear_resource_id_pool(1, 'ALLOC_ID')
165 self.assertFalse(status)
166
167
168if __name__ == '__main__':
169 main()