blob: a9a871c36e74ff1bf0b18aa3d2bbba697e837748 [file] [log] [blame]
Matt Jeanneret58f7a552018-10-02 07:00:27 -04001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14from copy import copy
15from random import randint, seed
16from time import time
17from unittest import main, TestCase
18import json
19
20from voltha.core.config.config_root import ConfigRoot
21from voltha.protos.openflow_13_pb2 import ofp_desc
22from voltha.protos.voltha_pb2 import VolthaInstance, HealthStatus, Adapter, \
23 AdapterConfig, LogicalDevice
24
25
26n_adapters = 1000
27n_logical_nodes = 1000
28
29
30class TestPersistence(TestCase):
31
32 def pump_some_data(self, node):
33 seed(0)
34 node.update('/', VolthaInstance(
35 instance_id='1',
36 version='42',
37 log_level=1
38 ))
39 node.update('/health', HealthStatus(state=HealthStatus.OVERLOADED))
40 for i in xrange(n_adapters):
41 node.add('/adapters', Adapter(
42 id=str(i),
43 vendor='cord',
44 version=str(randint(1, 10)),
45 config=AdapterConfig(
46 log_level=0
47 )
48 ))
49 for i in xrange(n_logical_nodes):
50 node.add('/logical_devices', LogicalDevice(
51 id=str(i),
52 datapath_id=randint(1, 100000),
53 desc=ofp_desc(
54 mfr_desc='foo',
55 hw_desc='bar',
56 sw_desc='zoo'
57 )
58 ))
59
60 def test_inmemory_kv_store(self):
61 t0 = [time()]
62 def pt(msg=''):
63 t1 = time()
64 print '%20.8f ms - %s' % (1000 * (t1 - t0[0]), msg)
65 t0[0] = t1
66
67 kv_store = dict()
68
69 # create node and pump data
70 node = ConfigRoot(VolthaInstance(), kv_store=kv_store)
71 node.tag('original')
72 pt('init')
73 self.pump_some_data(node)
74 pt('pump')
75 node.tag('pumped')
76
77 # check that content of kv_store looks ok
78 size1 = len(kv_store)
79 self.assertEqual(size1, 14 + 3 * (n_adapters + n_logical_nodes))
80
81 # this should actually drop if we pune
82 node.prune_untagged()
83 pt('prunning')
84
85 size2 = len(kv_store)
86 self.assertEqual(size2, 7 + 2 * (1 + 1 + n_adapters + n_logical_nodes) + 2)
87 all_latest_data = node.get('/', deep=1)
88 pt('deep get')
89
90 # save dict so that deleting the node will not wipe it
91 latest_hash = node.latest.hash
92 kv_store = copy(kv_store)
93 pt('copy kv store')
94 del node
95 pt('delete node')
96 # self.assertEqual(size2, 1 + 2 * (1 + 1 + n_adapters + n_logical_nodes))
97
98 self.assertEqual(json.loads(kv_store['root'])['latest'], latest_hash)
99 # recreate tree from persistence
100 node = ConfigRoot.load(VolthaInstance, kv_store)
101 pt('load from kv store')
102 self.assertEqual(node.get('/', deep=1), all_latest_data)
103 pt('deep get')
104 self.assertEqual(latest_hash, node.latest.hash)
105 self.assertEqual(node.tags, ['original', 'pumped'])
106
107
108if __name__ == '__main__':
109 main()