Merge from OSM SO master
[osm/SO.git] / rwlaunchpad / test / pytest / lp_tg_2vrouter_ts_epa_test.py
1 #!/usr/bin/env python
2 """
3 #
4 # Copyright 2016-2017 RIFT.IO Inc
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18
19 @file lp_3vnfs_test.py
20 @author Austin Cormier (Austin.Cormier@riftio.com)
21 @date 10/15/2015
22 @brief Launchpad Module Test ExtVNF
23 """
24
25 import json
26 import logging
27 import os
28 import pytest
29 import shlex
30 import requests
31 import subprocess
32 import time
33 import uuid
34
35 import gi
36 gi.require_version('RwIwpYang', '1.0')
37 gi.require_version('RwNsrYang', '1.0')
38 gi.require_version('RwProjectVnfdYang', '1.0')
39 gi.require_version('RwCloudYang', '1.0')
40 gi.require_version('RwBaseYang', '1.0')
41 gi.require_version('RwResourceMgrYang', '1.0')
42 gi.require_version('RwConmanYang', '1.0')
43 gi.require_version('RwNsmYang', '1.0')
44
45
46
47 from gi.repository import (
48 RwIwpYang,
49 ProjectNsdYang as NsdYang,
50 NsrYang,
51 RwNsrYang,
52 VldYang,
53 RwProjectVnfdYang as RwVnfdYang,
54 RwCloudYang,
55 RwBaseYang,
56 RwResourceMgrYang,
57 RwConmanYang,
58 RwNsmYang
59 )
60
61 logging.basicConfig(level=logging.DEBUG)
62
63
64 RW_VROUTER_PKG_INSTALL_DIR = os.path.join(
65 os.environ["RIFT_INSTALL"],
66 "usr/rift/mano/vnfds/vrouter"
67 )
68 RW_TRAFGEN_PKG_INSTALL_DIR = os.path.join(
69 os.environ["RIFT_INSTALL"],
70 "usr/rift/mano/vnfds/trafgen"
71 )
72 RW_TRAFSINK_PKG_INSTALL_DIR = os.path.join(
73 os.environ["RIFT_INSTALL"],
74 "usr/rift/mano/vnfds/trafsink"
75 )
76 RW_TG_2VROUTER_TS_NSD_PKG_INSTALL_DIR = os.path.join(
77 os.environ["RIFT_INSTALL"],
78 "usr/rift/mano/nsds/tg_2vrouter_ts"
79 )
80
81
82 class PackageError(Exception):
83 pass
84
85
86 def raise_package_error():
87 raise PackageError("Could not find ns packages")
88
89
90 @pytest.fixture(scope='module')
91 def iwp_proxy(request, mgmt_session):
92 return mgmt_session.proxy(RwIwpYang)
93
94 @pytest.fixture(scope='module')
95 def resource_mgr_proxy(request, mgmt_session):
96 return mgmt_session.proxy(RwResourceMgrYang)
97
98
99 @pytest.fixture(scope='module')
100 def cloud_proxy(request, mgmt_session):
101 return mgmt_session.proxy(RwCloudYang)
102
103
104 @pytest.fixture(scope='module')
105 def vnfd_proxy(request, mgmt_session):
106 return mgmt_session.proxy(RwVnfdYang)
107
108
109 @pytest.fixture(scope='module')
110 def vld_proxy(request, mgmt_session):
111 return mgmt_session.proxy(VldYang)
112
113
114 @pytest.fixture(scope='module')
115 def nsd_proxy(request, mgmt_session):
116 return mgmt_session.proxy(NsdYang)
117
118
119 @pytest.fixture(scope='module')
120 def nsr_proxy(request, mgmt_session):
121 return mgmt_session.proxy(NsrYang)
122
123
124 @pytest.fixture(scope='module')
125 def rwnsr_proxy(request, mgmt_session):
126 return mgmt_session.proxy(RwNsrYang)
127
128
129 @pytest.fixture(scope='module')
130 def base_proxy(request, mgmt_session):
131 return mgmt_session.proxy(RwBaseYang)
132
133 @pytest.fixture(scope='module')
134 def so_proxy(request, mgmt_session):
135 return mgmt_session.proxy(RwConmanYang)
136
137 @pytest.fixture(scope='module')
138 def nsm_proxy(request, mgmt_session):
139 return mgmt_session.proxy(RwNsmYang)
140
141 @pytest.fixture(scope='session')
142 def vrouter_vnfd_package_file():
143 vrouter_pkg_file = os.path.join(
144 RW_VROUTER_PKG_INSTALL_DIR,
145 "vrouter_vnfd_with_epa.tar.gz",
146 )
147 if not os.path.exists(vrouter_pkg_file):
148 raise_package_error()
149
150 return vrouter_pkg_file
151
152 @pytest.fixture(scope='session')
153 def tg_vnfd_package_file():
154 tg_pkg_file = os.path.join(
155 RW_TRAFGEN_PKG_INSTALL_DIR,
156 "trafgen_vnfd_with_epa.tar.gz",
157 )
158 if not os.path.exists(tg_pkg_file):
159 raise_package_error()
160
161 return tg_pkg_file
162
163 @pytest.fixture(scope='session')
164 def ts_vnfd_package_file():
165 ts_pkg_file = os.path.join(
166 RW_TRAFSINK_PKG_INSTALL_DIR,
167 "trafsink_vnfd_with_epa.tar.gz",
168 )
169 if not os.path.exists(ts_pkg_file):
170 raise_package_error()
171
172 return ts_pkg_file
173
174 @pytest.fixture(scope='session')
175 def tg_2vrouter_ts_nsd_package_file():
176 tg_2vrouter_ts_nsd_pkg_file = os.path.join(
177 RW_TG_2VROUTER_TS_NSD_PKG_INSTALL_DIR,
178 "tg_2vrouter_ts_nsd_with_epa.tar.gz",
179 )
180 if not os.path.exists(tg_2vrouter_ts_nsd_pkg_file):
181 raise_package_error()
182
183 return tg_2vrouter_ts_nsd_pkg_file
184
185
186 def create_nsr_from_nsd_id(nsd_id):
187 nsr = NsrYang.YangData_RwProject_Project_NsInstanceConfig_Nsr()
188 nsr.id = str(uuid.uuid4())
189 nsr.name = "TG-2Vrouter-TS EPA"
190 nsr.short_name = "TG-2Vrouter-TS EPA"
191 nsr.description = "4 VNFs with Trafgen, 2 Vrouters and Trafsink EPA"
192 nsr.nsd_ref = nsd_id
193 nsr.admin_status = "ENABLED"
194
195 return nsr
196
197
198 def upload_descriptor(logger, descriptor_file, host="127.0.0.1"):
199 curl_cmd = 'curl -F "descriptor=@{file}" http://{host}:4567/api/upload'.format(
200 file=descriptor_file,
201 host=host,
202 )
203 logger.debug("Uploading descriptor %s using cmd: %s", descriptor_file, curl_cmd)
204 stdout = subprocess.check_output(shlex.split(curl_cmd), universal_newlines=True)
205
206 json_out = json.loads(stdout)
207 transaction_id = json_out["transaction_id"]
208
209 return transaction_id
210
211
212 class DescriptorOnboardError(Exception):
213 pass
214
215
216 def wait_unboard_transaction_finished(logger, transaction_id, timeout_secs=600, host="127.0.0.1"):
217 logger.info("Waiting for onboard trans_id %s to complete",
218 transaction_id)
219 start_time = time.time()
220 while (time.time() - start_time) < timeout_secs:
221 r = requests.get(
222 'http://{host}:4567/api/upload/{t_id}/state'.format(
223 host=host, t_id=transaction_id
224 )
225 )
226 state = r.json()
227 if state["status"] == "pending":
228 time.sleep(1)
229 continue
230
231 elif state["status"] == "success":
232 logger.info("Descriptor onboard was successful")
233 return
234
235 else:
236 raise DescriptorOnboardError(state)
237
238 if state["status"] != "success":
239 raise DescriptorOnboardError(state)
240
241 @pytest.mark.incremental
242 class TestLaunchpadStartStop(object):
243 def test_configure_cloud_account(self, cloud_proxy, logger):
244 cloud_account = RwCloudYang.CloudAccountConfig()
245 #cloud_account.name = "cloudsim_proxy"
246 #cloud_account.account_type = "cloudsim_proxy"
247 cloud_account.name = "riftuser1"
248 cloud_account.account_type = "openstack"
249 cloud_account.openstack.key = 'pluto'
250 cloud_account.openstack.secret = 'mypasswd'
251 cloud_account.openstack.auth_url = 'http://10.66.4.xx:5000/v3/'
252 cloud_account.openstack.tenant = 'demo'
253 cloud_account.openstack.mgmt_network = 'private'
254
255 cloud_proxy.merge_config("/rw-project:project/rw-cloud:cloud-account", cloud_account)
256
257 def test_configure_pools(self, resource_mgr_proxy):
258 pools = RwResourceMgrYang.ResourcePools.from_dict({
259 "pools": [{ "name": "vm_pool_a",
260 "resource_type": "compute",
261 "pool_type" : "dynamic"},
262 {"name": "network_pool_a",
263 "resource_type": "network",
264 "pool_type" : "dynamic",}]})
265
266 resource_mgr_proxy.merge_config('/rw-project:project/rw-resource-mgr:resource-mgr-config/rw-resource-mgr:resource-pools', pools)
267
268 def test_configure_resource_orchestrator(self, so_proxy):
269 cfg = RwConmanYang.RoEndpoint.from_dict({'ro_ip_address': '127.0.0.1',
270 'ro_port' : 2022,
271 'ro_username' : 'admin',
272 'ro_password' : 'admin'})
273 so_proxy.merge_config('/rw-project:project/rw-conman:cm-config', cfg)
274
275 def test_configure_service_orchestrator(self, nsm_proxy):
276 cfg = RwNsmYang.SoEndpoint.from_dict({'cm_ip_address': '127.0.0.1',
277 'cm_port' : 2022,
278 'cm_username' : 'admin',
279 'cm_password' : 'admin'})
280 nsm_proxy.merge_config('/rw-nsm:ro-config/rw-nsm:cm-endpoint', cfg)
281
282
283 def test_onboard_tg_vnfd(self, logger, vnfd_proxy, tg_vnfd_package_file):
284 logger.info("Onboarding trafgen_vnfd package: %s", tg_vnfd_package_file)
285 trans_id = upload_descriptor(logger, tg_vnfd_package_file)
286 wait_unboard_transaction_finished(logger, trans_id)
287
288 catalog = vnfd_proxy.get_config('/vnfd-catalog')
289 vnfds = catalog.vnfd
290 assert len(vnfds) == 1, "There should be one vnfds"
291 assert "trafgen_vnfd" in [vnfds[0].name]
292
293 def test_onboard_vrouter_vnfd(self, logger, vnfd_proxy, vrouter_vnfd_package_file):
294 logger.info("Onboarding vrouter_vnfd package: %s", vrouter_vnfd_package_file)
295 trans_id = upload_descriptor(logger, vrouter_vnfd_package_file)
296 wait_unboard_transaction_finished(logger, trans_id)
297
298 catalog = vnfd_proxy.get_config('/vnfd-catalog')
299 vnfds = catalog.vnfd
300 assert len(vnfds) == 2, "There should be two vnfds"
301 assert "vrouter_vnfd" in [vnfds[0].name, vnfds[1].name]
302
303 def test_onboard_ts_vnfd(self, logger, vnfd_proxy, ts_vnfd_package_file):
304 logger.info("Onboarding trafsink_vnfd package: %s", ts_vnfd_package_file)
305 trans_id = upload_descriptor(logger, ts_vnfd_package_file)
306 wait_unboard_transaction_finished(logger, trans_id)
307
308 catalog = vnfd_proxy.get_config('/vnfd-catalog')
309 vnfds = catalog.vnfd
310 assert len(vnfds) == 3, "There should be three vnfds"
311 assert "trafsink_vnfd" in [vnfds[0].name, vnfds[1].name, vnfds[2].name]
312
313 def test_onboard_tg_2vrouter_ts_nsd(self, logger, nsd_proxy, tg_2vrouter_ts_nsd_package_file):
314 logger.info("Onboarding tg_2vrouter_ts nsd package: %s", tg_2vrouter_ts_nsd_package_file)
315 trans_id = upload_descriptor(logger, tg_2vrouter_ts_nsd_package_file)
316 wait_unboard_transaction_finished(logger, trans_id)
317
318 catalog = nsd_proxy.get_config('/nsd-catalog')
319 nsds = catalog.nsd
320 assert len(nsds) == 1, "There should only be a single nsd"
321 nsd = nsds[0]
322 assert nsd.name == "tg_vrouter_ts_nsd"
323 assert nsd.short_name == "tg_2vrouter_ts_nsd"
324
325 def test_instantiate_tg_2vrouter_ts_nsr(self, logger, nsd_proxy, nsr_proxy, rwnsr_proxy, base_proxy):
326 catalog = nsd_proxy.get_config('/nsd-catalog')
327 nsd = catalog.nsd[0]
328
329 nsr = create_nsr_from_nsd_id(nsd.id)
330 nsr_proxy.merge_config('/ns-instance-config', nsr)
331
332 nsr_opdata = rwnsr_proxy.get('/ns-instance-opdata')
333 nsrs = nsr_opdata.nsr
334 assert len(nsrs) == 1
335 assert nsrs[0].ns_instance_config_ref == nsr.id
336
337