3 # Copyright 2016 RIFT.IO Inc
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
26 import rift
.mano
.dts
as store
29 gi
.require_version('RwDtsYang', '1.0')
30 from gi
.repository
import (
31 RwLaunchpadYang
as launchpadyang
,
41 class DescriptorPublisher(object):
42 def __init__(self
, log
, dts
, loop
):
47 self
._registrations
= []
50 def publish(self
, w_path
, path
, desc
):
51 ready_event
= asyncio
.Event(loop
=self
.loop
)
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
)
62 handler
= rift
.tasklets
.DTS
.RegistrationHandler(
66 self
.log
.debug("Registering path: %s, obj:%s", w_path
, desc
)
67 reg
= yield from self
.dts
.register(
70 flags
=rwdts
.Flag
.PUBLISHER | rwdts
.Flag
.NO_PREP_READ
72 self
._registrations
.append(reg
)
73 self
.log
.debug("Registered path : %s", w_path
)
74 yield from ready_event
.wait()
78 def unpublish_all(self
):
79 self
.log
.debug("Deregistering all published descriptors")
80 for reg
in self
._registrations
:
83 class SubscriberStoreDtsTestCase(rift
.test
.dts
.AbstractDTSTest
):
85 def configure_schema(cls
):
86 return launchpadyang
.get_schema()
89 def configure_timeout(cls
):
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
)
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
)
100 self
.store
= store
.SubscriberStore(self
.log
, self
.dts
, self
.loop
)
101 self
.publisher
= DescriptorPublisher(self
.log
, self
.dts
, self
.loop
)
106 @rift.test
.dts
.async_test
107 def test_vnfd_handler(self
):
108 yield from self
.store
.register()
110 mock_vnfd
= RwVnfdYang
.YangData_RwProject_Project_VnfdCatalog_Vnfd()
111 mock_vnfd
.id = str(uuid
.uuid1())
113 w_xpath
= "C,/rw-project:project/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
)
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
121 yield from self
.dts
.query_update(xpath
, rwdts
.XactFlag
.ADVISE
, mock_vnfd
)
122 assert len(self
.store
.vnfd
) == 1
124 yield from self
.dts
.query_delete(xpath
, flags
=rwdts
.XactFlag
.ADVISE
)
125 assert len(self
.store
.vnfd
) == 0
127 @rift.test
.dts
.async_test
128 def test_vnfr_handler(self
):
129 yield from self
.store
.register()
131 mock_vnfr
= RwVnfrYang
.YangData_RwProject_Project_VnfrCatalog_Vnfr()
132 mock_vnfr
.id = str(uuid
.uuid1())
134 w_xpath
= "D,/rw-project:project/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
)
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
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
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
150 @rift.test
.dts
.async_test
151 def test_nsr_handler(self
):
152 yield from self
.store
.register()
154 mock_nsr
= RwNsrYang
.YangData_RwProject_Project_NsInstanceOpdata_Nsr()
155 mock_nsr
.ns_instance_config_ref
= str(uuid
.uuid1())
156 mock_nsr
.name_ref
= "Foo"
158 w_xpath
= "D,/rw-project:project/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
)
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
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
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
174 @rift.test
.dts
.async_test
175 def test_nsd_handler(self
):
176 yield from self
.store
.register()
178 mock_nsd
= RwNsdYang
.YangData_RwProject_Project_NsdCatalog_Nsd()
179 mock_nsd
.id = str(uuid
.uuid1())
181 w_xpath
= "C,/rw-project:project/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
)
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
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
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
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
204 vnf_handler
.get_reg_flags
= types
.MethodType(get_reg_flags
, vnf_handler
)
207 yield from vnf_handler
.register()
209 mock_vnfr
= RwVnfrYang
.YangData_RwProject_Project_VnfrCatalog_Vnfr()
210 mock_vnfr
.id = str(uuid
.uuid1())
212 def mon_xpath(param_id
=None):
213 """ Monitoring params xpath """
214 return("D,/rw-project:project/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 ""))
220 w_xpath
= "D,/rw-project:project/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
)
224 mock_param
= VnfrYang
.YangData_RwProject_Project_VnfrCatalog_Vnfr_MonitoringParam
.from_dict({
227 mock_vnfr
.monitoring_param
.append(mock_param
)
228 yield from self
.publisher
.publish(w_xpath
, xpath
, mock_vnfr
)
230 def main(argv
=sys
.argv
[1:]):
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).
236 argv
=[__file__
] + argv
,
237 testRunner
=None#xmlrunner.XMLTestRunner(output=os.environ["RIFT_MODULE_TEST"])
240 if __name__
== '__main__':