a69a00f50e71fac6c993f282f88673d51ecd7937
[osm/SO.git] / common / python / rift / mano / dts / subscriber / test / utest_subscriber_dts.py
1
2 #
3 # Copyright 2016 RIFT.IO Inc
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18 import asyncio
19 import sys
20 import types
21 import unittest
22 import uuid
23
24
25 import rift.test.dts
26 import rift.mano.dts as store
27
28 import gi
29 gi.require_version('RwDtsYang', '1.0')
30 from gi.repository import (
31 RwLaunchpadYang as launchpadyang,
32 RwDts as rwdts,
33 RwVnfdYang,
34 RwVnfrYang,
35 RwNsrYang,
36 RwNsdYang,
37 VnfrYang
38 )
39
40
41 class DescriptorPublisher(object):
42 def __init__(self, log, dts, loop):
43 self.log = log
44 self.loop = loop
45 self.dts = dts
46
47 self._registrations = []
48
49 @asyncio.coroutine
50 def publish(self, w_path, path, desc):
51 ready_event = asyncio.Event(loop=self.loop)
52
53 @asyncio.coroutine
54 def on_ready(regh, status):
55 self.log.debug("Create element: %s, obj-type:%s obj:%s",
56 path, type(desc), desc)
57 with self.dts.transaction() as xact:
58 regh.create_element(path, desc, xact.xact)
59 self.log.debug("Created element: %s, obj:%s", path, desc)
60 ready_event.set()
61
62 handler = rift.tasklets.DTS.RegistrationHandler(
63 on_ready=on_ready
64 )
65
66 self.log.debug("Registering path: %s, obj:%s", w_path, desc)
67 reg = yield from self.dts.register(
68 w_path,
69 handler,
70 flags=rwdts.Flag.PUBLISHER | rwdts.Flag.NO_PREP_READ
71 )
72 self._registrations.append(reg)
73 self.log.debug("Registered path : %s", w_path)
74 yield from ready_event.wait()
75
76 return reg
77
78 def unpublish_all(self):
79 self.log.debug("Deregistering all published descriptors")
80 for reg in self._registrations:
81 reg.deregister()
82
83 class SubscriberStoreDtsTestCase(rift.test.dts.AbstractDTSTest):
84 @classmethod
85 def configure_schema(cls):
86 return launchpadyang.get_schema()
87
88 @classmethod
89 def configure_timeout(cls):
90 return 240
91
92 def configure_test(self, loop, test_id):
93 self.log.debug("STARTING - %s", test_id)
94 self.tinfo = self.new_tinfo(str(test_id))
95 self.dts = rift.tasklets.DTS(self.tinfo, self.schema, self.loop)
96
97 self.tinfo_sub = self.new_tinfo(str(test_id) + "_sub")
98 self.dts_sub = rift.tasklets.DTS(self.tinfo_sub, self.schema, self.loop)
99
100 self.store = store.SubscriberStore(self.log, self.dts, self.loop)
101 self.publisher = DescriptorPublisher(self.log, self.dts, self.loop)
102
103 def tearDown(self):
104 super().tearDown()
105
106 @rift.test.dts.async_test
107 def test_vnfd_handler(self):
108 yield from self.store.register()
109
110 mock_vnfd = RwVnfdYang.YangData_Vnfd_VnfdCatalog_Vnfd()
111 mock_vnfd.id = str(uuid.uuid1())
112
113 w_xpath = "C,/vnfd:vnfd-catalog/vnfd:vnfd"
114 xpath = "{}[vnfd:id='{}']".format(w_xpath, mock_vnfd.id)
115 yield from self.publisher.publish(w_xpath, xpath, mock_vnfd)
116
117 yield from asyncio.sleep(5, loop=self.loop)
118 assert len(self.store.vnfd) == 1
119 assert self.store.get_vnfd(self.store.vnfd[0].id) is not None
120
121 yield from self.dts.query_update(xpath, rwdts.XactFlag.ADVISE, mock_vnfd)
122 assert len(self.store.vnfd) == 1
123
124 yield from self.dts.query_delete(xpath, flags=rwdts.XactFlag.ADVISE)
125 assert len(self.store.vnfd) == 0
126
127 @rift.test.dts.async_test
128 def test_vnfr_handler(self):
129 yield from self.store.register()
130
131 mock_vnfr = RwVnfrYang.YangData_Vnfr_VnfrCatalog_Vnfr()
132 mock_vnfr.id = str(uuid.uuid1())
133
134 w_xpath = "D,/vnfr:vnfr-catalog/vnfr:vnfr"
135 xpath = "{}[vnfr:id='{}']".format(w_xpath, mock_vnfr.id)
136 yield from self.publisher.publish(w_xpath, xpath, mock_vnfr)
137
138 yield from asyncio.sleep(5, loop=self.loop)
139 assert len(self.store.vnfr) == 1
140 assert self.store.get_vnfr(self.store.vnfr[0].id) is not None
141
142 yield from self.dts.query_update(xpath, rwdts.XactFlag.ADVISE, mock_vnfr)
143 yield from asyncio.sleep(5, loop=self.loop)
144 assert len(self.store.vnfr) == 1
145
146 yield from self.dts.query_delete(xpath, flags=rwdts.XactFlag.ADVISE)
147 yield from asyncio.sleep(5, loop=self.loop)
148 assert len(self.store.vnfr) == 0
149
150 @rift.test.dts.async_test
151 def test_nsr_handler(self):
152 yield from self.store.register()
153
154 mock_nsr = RwNsrYang.YangData_Nsr_NsInstanceOpdata_Nsr()
155 mock_nsr.ns_instance_config_ref = str(uuid.uuid1())
156 mock_nsr.name_ref = "Foo"
157
158 w_xpath = "D,/nsr:ns-instance-opdata/nsr:nsr"
159 xpath = "{}[nsr:ns-instance-config-ref='{}']".format(w_xpath, mock_nsr.ns_instance_config_ref)
160 yield from self.publisher.publish(w_xpath, xpath, mock_nsr)
161
162 yield from asyncio.sleep(5, loop=self.loop)
163 assert len(self.store.nsr) == 1
164 assert self.store.get_nsr(self.store.nsr[0].ns_instance_config_ref) is not None
165
166 yield from self.dts.query_update(xpath, rwdts.XactFlag.ADVISE, mock_nsr)
167 yield from asyncio.sleep(5, loop=self.loop)
168 assert len(self.store.nsr) == 1
169
170 yield from self.dts.query_delete(xpath, flags=rwdts.XactFlag.ADVISE)
171 yield from asyncio.sleep(5, loop=self.loop)
172 assert len(self.store.nsr) == 0
173
174 @rift.test.dts.async_test
175 def test_nsd_handler(self):
176 yield from self.store.register()
177
178 mock_nsd = RwNsdYang.YangData_Nsd_NsdCatalog_Nsd()
179 mock_nsd.id = str(uuid.uuid1())
180
181 w_xpath = "C,/nsd:nsd-catalog/nsd:nsd"
182 xpath = "{}[nsd:id='{}']".format(w_xpath, mock_nsd.id)
183 yield from self.publisher.publish(w_xpath, xpath, mock_nsd)
184
185 yield from asyncio.sleep(2, loop=self.loop)
186 assert len(self.store.nsd) == 1
187 assert self.store.get_nsd(self.store.nsd[0].id) is not None
188
189 yield from self.dts.query_update(xpath, rwdts.XactFlag.ADVISE, mock_nsd)
190 yield from asyncio.sleep(5, loop=self.loop)
191 assert len(self.store.nsd) == 1
192
193 yield from self.dts.query_delete(xpath, flags=rwdts.XactFlag.ADVISE)
194 yield from asyncio.sleep(5, loop=self.loop)
195 assert len(self.store.nsd) == 0
196
197 @rift.test.dts.async_test
198 def test_vnfr_crash(self):
199 vnf_handler = store.VnfrCatalogSubscriber(self.log, self.dts, self.loop)
200 def get_reg_flags(self):
201 from gi.repository import RwDts as rwdts
202 return rwdts.Flag.SUBSCRIBER|rwdts.Flag.DELTA_READY|rwdts.Flag.CACHE
203
204 vnf_handler.get_reg_flags = types.MethodType(get_reg_flags, vnf_handler)
205
206 # publish
207 yield from vnf_handler.register()
208
209 mock_vnfr = RwVnfrYang.YangData_Vnfr_VnfrCatalog_Vnfr()
210 mock_vnfr.id = str(uuid.uuid1())
211
212 def mon_xpath(param_id=None):
213 """ Monitoring params xpath """
214 return("D,/vnfr:vnfr-catalog" +
215 "/vnfr:vnfr[vnfr:id='{}']".format(mock_vnfr.id) +
216 "/vnfr:monitoring-param" +
217 ("[vnfr:id='{}']".format(param_id) if param_id else ""))
218
219
220 w_xpath = "D,/vnfr:vnfr-catalog/vnfr:vnfr"
221 xpath = "{}[vnfr:id='{}']".format(w_xpath, mock_vnfr.id)
222 yield from self.publisher.publish(w_xpath, xpath, mock_vnfr)
223
224 mock_param = VnfrYang.YangData_Vnfr_VnfrCatalog_Vnfr_MonitoringParam.from_dict({
225 "id": "1"
226 })
227 mock_vnfr.monitoring_param.append(mock_param)
228 yield from self.publisher.publish(w_xpath, xpath, mock_vnfr)
229
230 def main(argv=sys.argv[1:]):
231
232 # The unittest framework requires a program name, so use the name of this
233 # file instead (we do not want to have to pass a fake program name to main
234 # when this is called from the interpreter).
235 unittest.main(
236 argv=[__file__] + argv,
237 testRunner=None#xmlrunner.XMLTestRunner(output=os.environ["RIFT_MODULE_TEST"])
238 )
239
240 if __name__ == '__main__':
241 main()