blob: 6c08fe82eba2bc9504e26fcd0463964b86b0e66d [file] [log] [blame]
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -04001
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
18import asyncio
19import sys
20import types
21import unittest
22import uuid
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -040023import os
24import xmlrunner
25
26#Setting RIFT_VAR_ROOT if not already set for unit test execution
27if "RIFT_VAR_ROOT" not in os.environ:
28 os.environ['RIFT_VAR_ROOT'] = os.path.join(os.environ['RIFT_INSTALL'], 'var/rift/unittest')
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040029
30import rift.test.dts
31import rift.tasklets.rwnsmtasklet.cloud as cloud
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -040032import rift.tasklets.rwnsmtasklet.rwnsmplugin as rwnsmplugin
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040033import rift.tasklets.rwnsmtasklet.openmano_nsm as openmano_nsm
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -040034from rift.mano.utils.project import ManoProject
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040035import rw_peas
36
37import gi
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -040038gi.require_version('RwDts', '1.0')
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040039from gi.repository import (
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -040040 RwRoAccountYang as roaccountyang,
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040041 RwDts as rwdts,
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -040042 RwProjectVnfdYang as RwVnfdYang,
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040043 RwVnfrYang,
44 RwNsrYang,
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -040045 RwProjectNsdYang as RwNsdYang,
46 VnfrYang,
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040047 )
48
49
50class DescriptorPublisher(object):
51 def __init__(self, log, dts, loop):
52 self.log = log
53 self.loop = loop
54 self.dts = dts
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040055 self._registrations = []
56
57 @asyncio.coroutine
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -040058 def update(self, xpath, desc):
59 self._registrations[-1].update_element(xpath, desc)
60
61 @asyncio.coroutine
62 def delete(self, xpath):
63 self._registrations[-1].delete_element(xpath)
64
65 @asyncio.coroutine
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040066 def publish(self, w_path, path, desc):
67 ready_event = asyncio.Event(loop=self.loop)
68
69 @asyncio.coroutine
70 def on_ready(regh, status):
71 self.log.debug("Create element: %s, obj-type:%s obj:%s",
72 path, type(desc), desc)
73 with self.dts.transaction() as xact:
74 regh.create_element(path, desc, xact.xact)
75 self.log.debug("Created element: %s, obj:%s", path, desc)
76 ready_event.set()
77
78 handler = rift.tasklets.DTS.RegistrationHandler(
79 on_ready=on_ready
80 )
81
82 self.log.debug("Registering path: %s, obj:%s", w_path, desc)
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -040083
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040084 reg = yield from self.dts.register(
85 w_path,
86 handler,
87 flags=rwdts.Flag.PUBLISHER | rwdts.Flag.NO_PREP_READ
88 )
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -040089
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040090 self._registrations.append(reg)
91 self.log.debug("Registered path : %s", w_path)
92 yield from ready_event.wait()
93
94 return reg
95
96 def unpublish_all(self):
97 self.log.debug("Deregistering all published descriptors")
98 for reg in self._registrations:
99 reg.deregister()
100
101class RoAccountDtsTestCase(rift.test.dts.AbstractDTSTest):
102 @classmethod
103 def configure_schema(cls):
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -0400104 return roaccountyang.get_schema()
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400105
106 @classmethod
107 def configure_timeout(cls):
108 return 240
109
110 def configure_test(self, loop, test_id):
111 self.log.debug("STARTING - %s", test_id)
112 self.tinfo = self.new_tinfo(str(test_id))
113 self.dts = rift.tasklets.DTS(self.tinfo, self.schema, self.loop)
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -0400114 self.project = ManoProject(self.log)
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400115
116 self.tinfo_sub = self.new_tinfo(str(test_id) + "_sub")
117 self.dts_sub = rift.tasklets.DTS(self.tinfo_sub, self.schema, self.loop)
118
119 self.publisher = DescriptorPublisher(self.log, self.dts, self.loop)
120
121 def tearDown(self):
122 super().tearDown()
123
124 @rift.test.dts.async_test
125 def test_orch_account_create(self):
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -0400126 ro_cfg_sub = cloud.ROAccountConfigSubscriber(self.dts, self.log, self.loop, self.project, None)
127 yield from ro_cfg_sub.register()
128
129 ro_plugin = ro_cfg_sub.get_ro_plugin(account_name=None)
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400130 # Test if we have a default plugin in case no RO is specified.
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -0400131 assert type(ro_plugin) is rwnsmplugin.RwNsPlugin
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400132
Varun Prasadbe1e6292016-10-04 04:41:07 -0400133 # Test rift-ro plugin CREATE
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -0400134 w_xpath = self.project.add_project("C,/rw-ro-account:ro-account/rw-ro-account:account")
135 xpath = w_xpath + "[rw-ro-account:name='openmano']"
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400136
Varun Prasadbe1e6292016-10-04 04:41:07 -0400137 # Test Openmano plugin CREATE
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -0400138 mock_orch_acc = roaccountyang.YangData_RwProject_Project_RoAccount_Account.from_dict(
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400139 {'name': 'openmano',
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -0400140 'ro_account_type': 'openmano',
Varun Prasadbe1e6292016-10-04 04:41:07 -0400141 'openmano': {'tenant_id': "abc",
142 "port": 9999,
143 "host": "10.64.11.77"}})
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -0400144
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400145 yield from self.publisher.publish(w_xpath, xpath, mock_orch_acc)
146 yield from asyncio.sleep(5, loop=self.loop)
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -0400147
148 ro_plugin = ro_cfg_sub.get_ro_plugin(account_name='openmano')
149 assert type(ro_plugin) is openmano_nsm.OpenmanoNsPlugin
Varun Prasadbe1e6292016-10-04 04:41:07 -0400150
151 # Test update
152 mock_orch_acc.openmano.port = 9789
153 mock_orch_acc.openmano.host = "10.64.11.78"
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -0400154 yield from self.publisher.update(xpath, mock_orch_acc)
155 yield from asyncio.sleep(5, loop=self.loop)
Varun Prasadbe1e6292016-10-04 04:41:07 -0400156
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -0400157 #Since update means delete followed by a insert get the new ro_plugin.
158 ro_plugin = ro_cfg_sub.get_ro_plugin(account_name='openmano')
159 assert ro_plugin._cli_api._port == mock_orch_acc.openmano.port
160 assert ro_plugin._cli_api._host == mock_orch_acc.openmano.host
Varun Prasadbe1e6292016-10-04 04:41:07 -0400161
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -0400162 # Test delete to be implemented. right now facing some dts issues.
163 # Use DescriptorPublisher delete for deletion
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400164
165def main(argv=sys.argv[1:]):
166
167 # The unittest framework requires a program name, so use the name of this
168 # file instead (we do not want to have to pass a fake program name to main
169 # when this is called from the interpreter).
170 unittest.main(
171 argv=[__file__] + argv,
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -0400172 testRunner=xmlrunner.XMLTestRunner(output=os.environ["RIFT_MODULE_TEST"])
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400173 )
174
175if __name__ == '__main__':
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -0400176 main()