3 # Copyright (c) 2016 Orange and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
19 import functest.core.vnf as vnf
20 import functest.utils.openstack_utils as os_utils
21 from functest.utils.constants import CONST
23 from org.openbaton.cli.agents.agents import MainAgent
24 from org.openbaton.cli.errors.errors import NfvoException
27 __author__ = "Pauls, Michael <michael.pauls@fokus.fraunhofer.de>"
28 # ----------------------------------------------------------
32 # -----------------------------------------------------------
35 def get_config(parameter, my_file):
37 Returns the value of a given parameter in file.yaml
38 parameter must be given in string format with dots
39 Example: general.openstack.image_name
42 file_yaml = yaml.safe_load(f)
45 for element in parameter.split("."):
46 value = value.get(element)
48 raise ValueError("The parameter %s is not defined in"
49 " %s" % (parameter, my_file))
53 def servertest(host, port):
54 args = socket.getaddrinfo(host, port, socket.AF_INET, socket.SOCK_STREAM)
55 for family, socktype, proto, canonname, sockaddr in args:
56 s = socket.socket(family, socktype, proto)
66 class ImsVnf(vnf.VnfOnBoarding):
67 """OpenIMS VNF deployed with openBaton orchestrator"""
69 def __init__(self, project='functest', case_name='orchestra_ims',
71 super(ImsVnf, self).__init__(project, case_name, repo, cmd)
72 self.logger = logging.getLogger(__name__)
73 self.logger.info("Orchestra IMS VNF onboarding test starting")
74 self.ob_password = "openbaton"
75 self.ob_username = "admin"
78 self.ob_ip = "localhost"
79 self.ob_instance_id = ""
80 self.case_dir = pkg_resources.resource_filename(
81 'functest', 'opnfv_tests/vnf/ims/')
82 self.data_dir = CONST.__getattribute__('dir_ims_data')
83 self.test_dir = CONST.__getattribute__('dir_repo_vims_test')
84 self.ob_projectid = ""
85 self.keystone_client = os_utils.get_keystone_client()
88 self.main_agent = None
89 # vIMS Data directory creation
90 if not os.path.exists(self.data_dir):
91 os.makedirs(self.data_dir)
92 # Retrieve the configuration
94 self.config = CONST.__getattribute__(
95 'vnf_{}_config'.format(self.case_name))
97 raise Exception("Orchestra VNF config file not found")
98 config_file = self.case_dir + self.config
99 self.imagename = get_config("openbaton.imagename", config_file)
100 self.bootstrap_link = get_config("openbaton.bootstrap_link",
102 self.bootstrap_config_link = get_config(
103 "openbaton.bootstrap_config_link", config_file)
104 self.market_link = get_config("openbaton.marketplace_link",
106 self.images = get_config("tenant_images", config_file)
107 self.ims_conf = get_config("vIMS", config_file)
108 self.userdata_file = get_config("openbaton.userdata.file",
111 def deploy_orchestrator(self):
112 self.logger.info("Additional pre-configuration steps")
113 nova_client = os_utils.get_nova_client()
114 neutron_client = os_utils.get_neutron_client()
115 glance_client = os_utils.get_glance_client()
117 # Import images if needed
119 self.logger.info("Upload some OS images if it doesn't exist")
120 temp_dir = os.path.join(self.data_dir, "tmp/")
121 for image_name, image_url in self.images.iteritems():
122 self.logger.info("image: %s, url: %s", image_name, image_url)
124 image_id = os_utils.get_image_id(glance_client,
126 self.logger.info("image_id: %s", image_id)
127 except BaseException:
128 self.logger.error("Unexpected error: %s", sys.exc_info()[0])
131 self.logger.info("""%s image doesn't exist on glance
132 repository. Try downloading this image
133 and upload on glance !""" % image_name)
134 image_id = os_utils.download_and_add_image_on_glance(
140 self.logger.error("Failed to find or upload required OS "
141 "image for this deployment")
144 network_dic = os_utils.create_network_full(neutron_client,
146 "openbaton_mgmt_subnet",
150 # orchestrator VM flavor
152 "Check if orchestra Flavor is available, if not create one")
153 flavor_exist, flavor_id = os_utils.get_or_create_flavor(
159 self.logger.debug("Flavor id: %s" % flavor_id)
162 self.logger.error("There has been a problem when creating the "
165 network_id = network_dic["net_id"]
167 self.logger.info("Creating floating IP for VM in advance...")
168 floatip_dic = os_utils.create_floating_ip(neutron_client)
169 floatip = floatip_dic['fip_addr']
172 self.logger.error("Cannot create floating IP.")
175 userdata = "#!/bin/bash\n"
176 userdata += "echo \"Executing userdata...\"\n"
177 userdata += "set -x\n"
178 userdata += "set -e\n"
179 userdata += "echo \"Set nameserver to '8.8.8.8'...\"\n"
180 userdata += "echo \"nameserver 8.8.8.8\" >> /etc/resolv.conf\n"
181 userdata += "echo \"Install curl...\"\n"
182 userdata += "apt-get install curl\n"
183 userdata += "echo \"Inject public key...\"\n"
184 userdata += ("echo \"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCuPXrV3"
185 "geeHc6QUdyUr/1Z+yQiqLcOskiEGBiXr4z76MK4abiFmDZ18OMQlc"
186 "fl0p3kS0WynVgyaOHwZkgy/DIoIplONVr2CKBKHtPK+Qcme2PVnCtv"
187 "EqItl/FcD+1h5XSQGoa+A1TSGgCod/DPo+pes0piLVXP8Ph6QS1k7S"
188 "ic7JDeRQ4oT1bXYpJ2eWBDMfxIWKZqcZRiGPgMIbJ1iEkxbpeaAd9O"
189 "4MiM9nGCPESmed+p54uYFjwEDlAJZShcAZziiZYAvMZhvAhe6USljc"
190 "7YAdalAnyD/jwCHuwIrUw/lxo7UdNCmaUxeobEYyyFA1YVXzpNFZya"
191 "XPGAAYIJwEq/ openbaton@opnfv\" >> /home/ubuntu/.ssh/aut"
193 userdata += "echo \"Download bootstrap...\"\n"
194 userdata += ("curl -s %s "
195 "> ./bootstrap\n" % self.bootstrap_link)
196 userdata += ("curl -s %s"
197 "> ./config_file\n" % self.bootstrap_config_link)
198 userdata += ("echo \"Disable usage of mysql...\"\n")
199 userdata += "sed -i s/mysql=.*/mysql=no/g /config_file\n"
200 userdata += ("echo \"Setting 'rabbitmq_broker_ip' to '%s'\"\n"
202 userdata += ("sed -i s/rabbitmq_broker_ip=localhost/rabbitmq_broker_ip"
203 "=%s/g /config_file\n" % floatip)
204 userdata += "echo \"Set autostart of components to 'false'\"\n"
205 userdata += "export OPENBATON_COMPONENT_AUTOSTART=false\n"
206 userdata += "echo \"Execute bootstrap...\"\n"
207 bootstrap = "sh ./bootstrap release -configFile=./config_file"
208 userdata += bootstrap + "\n"
209 userdata += "echo \"Setting 'nfvo.plugin.timeout' to '300000'\"\n"
210 userdata += ("echo \"nfvo.plugin.timeout=600000\" >> "
211 "/etc/openbaton/openbaton-nfvo.properties\n")
213 "wget %s -O /etc/openbaton/openbaton-vnfm-generic-user-data.sh\n" %
215 userdata += "sed -i '113i\ \ \ \ sleep 60' " \
216 "/etc/openbaton/openbaton-vnfm-generic-user-data.sh\n"
217 userdata += "echo \"Starting NFVO\"\n"
218 userdata += "service openbaton-nfvo restart\n"
219 userdata += "echo \"Starting Generic VNFM\"\n"
220 userdata += "service openbaton-vnfm-generic restart\n"
221 userdata += "echo \"...end of userdata...\"\n"
223 sg_id = os_utils.create_security_group_full(neutron_client,
224 "orchestra-sec-group",
227 os_utils.create_secgroup_rule(neutron_client, sg_id, "ingress",
229 os_utils.create_secgroup_rule(neutron_client, sg_id, "egress",
231 os_utils.create_secgroup_rule(neutron_client, sg_id, "ingress",
233 os_utils.create_secgroup_rule(neutron_client, sg_id, "ingress",
235 os_utils.create_secgroup_rule(neutron_client, sg_id, "egress",
237 os_utils.create_secgroup_rule(neutron_client, sg_id, "egress",
240 self.logger.info("Security group set")
242 self.logger.info("Create instance....")
243 self.logger.info("flavor: m1.medium\n"
251 instance = os_utils.create_instance_and_wait_for_active(
253 os_utils.get_image_id(glance_client, self.imagename),
255 "orchestra-openbaton",
259 self.ob_instance_id = instance.id
261 self.logger.info("Adding sec group to orchestra instance")
262 os_utils.add_secgroup_to_instance(nova_client,
263 self.ob_instance_id, sg_id)
265 self.logger.info("Associating floating ip: '%s' to VM '%s' ",
267 "orchestra-openbaton")
268 if not os_utils.add_floating_ip(nova_client, instance.id, floatip):
269 self.logger.error("Cannot associate floating IP to VM.")
272 self.logger.info("Waiting for Open Baton NFVO to be up and running...")
275 if servertest(floatip, "8080"):
279 "Open Baton NFVO is not started yet (%ss)" %
285 self.logger.error("Open Baton is not started correctly")
288 self.ob_password = "openbaton"
289 self.ob_username = "admin"
290 self.ob_https = False
291 self.ob_port = "8080"
292 self.logger.info("Waiting for all components up and running...")
294 self.details["orchestrator"] = {
295 'status': "PASS", 'result': "Deploy Open Baton NFVO: OK"}
296 self.logger.info("Deploy Open Baton NFVO: OK")
299 def deploy_vnf(self):
300 self.logger.info("Starting vIMS Deployment...")
302 self.main_agent = MainAgent(nfvo_ip=self.ob_ip,
303 nfvo_port=self.ob_port,
306 username=self.ob_username,
307 password=self.ob_password)
310 "Check if openims Flavor is available, if not create one")
311 flavor_exist, flavor_id = os_utils.get_or_create_flavor(
317 self.logger.debug("Flavor id: %s", flavor_id)
319 self.logger.info("Getting project 'default'...")
320 project_agent = self.main_agent.get_agent("project", self.ob_projectid)
321 for p in json.loads(project_agent.find()):
322 if p.get("name") == "default":
323 self.ob_projectid = p.get("id")
324 self.logger.info("Found project 'default': %s", p)
327 self.logger.debug("project id: %s", self.ob_projectid)
328 if self.ob_projectid == "":
329 self.logger.error("Default project id was not found!")
331 creds = os_utils.get_credentials()
332 self.logger.info("PoP creds: %s", creds)
334 if os_utils.is_keystone_v3():
336 "Using v3 API of OpenStack... -> Using OS_PROJECT_ID")
337 project_id = os_utils.get_tenant_id(
338 os_utils.get_keystone_client(),
339 creds.get("project_name"))
342 "Using v2 API of OpenStack... -> Using OS_TENANT_NAME")
343 project_id = creds.get("tenant_name")
345 self.logger.debug("project id: %s", project_id)
348 "name": "vim-instance",
349 "authUrl": creds.get("auth_url"),
350 "tenant": project_id,
351 "username": creds.get("username"),
352 "password": creds.get("password"),
355 "orchestra-sec-group"
360 "latitude": "52.525876",
361 "longitude": "13.314400"
365 self.logger.debug("Registering VIM: %s", vim_json)
367 self.main_agent.get_agent(
369 project_id=self.ob_projectid).create(entity=json.dumps(vim_json))
371 market_agent = self.main_agent.get_agent("market",
372 project_id=self.ob_projectid)
376 self.logger.info("sending: %s", self.market_link)
377 nsd = market_agent.create(entity=self.market_link)
378 self.logger.info("Onboarded NSD: " + nsd.get("name"))
379 except NfvoException as e:
380 self.logger.error(e.message)
382 nsr_agent = self.main_agent.get_agent("nsr",
383 project_id=self.ob_projectid)
384 nsd_id = nsd.get('id')
386 self.logger.error("NSD not onboarded correctly")
389 self.nsr = nsr_agent.create(nsd_id)
390 except NfvoException as e:
391 self.logger.error(e.message)
393 if self.nsr.get('code') is not None:
395 "vIMS cannot be deployed: %s -> %s",
396 self.nsr.get('code'),
397 self.nsr.get('message'))
398 self.logger.error("vIMS cannot be deployed")
401 self.logger.info("Waiting for NSR to go to ACTIVE...")
402 while self.nsr.get("status") != 'ACTIVE' and self.nsr.get(
403 "status") != 'ERROR':
406 self.logger.error("INACTIVE NSR after %s sec..", 5 * i)
409 self.nsr = json.loads(nsr_agent.find(self.nsr.get('id')))
411 if self.nsr.get("status") == 'ACTIVE':
412 self.details["vnf"] = {'status': "PASS", 'result': self.nsr}
413 self.logger.info("Deploy VNF: OK")
415 self.details["vnf"] = {'status': "FAIL", 'result': self.nsr}
416 self.logger.error(self.nsr)
417 self.logger.error("Deploy VNF: ERROR")
420 self.ob_nsr_id = self.nsr.get("id")
422 "Sleep for 60s to ensure that all services are up and running...")
427 # Adaptations probably needed
428 # code used for cloudify_ims
429 # ruby client on jumphost calling the vIMS on the SUT
431 "Testing if %s works properly...", self.nsr.get('name'))
432 for vnfr in self.nsr.get('vnfr'):
434 "Checking ports %s of VNF %s",
435 self.ims_conf.get(vnfr.get('name')).get('ports'),
437 for vdu in vnfr.get('vdu'):
438 for vnfci in vdu.get('vnfc_instance'):
440 "Checking ports of VNFC instance %s",
441 vnfci.get('hostname'))
442 for floatingIp in vnfci.get('floatingIps'):
445 vnfci.get('hostname'),
446 floatingIp.get('ip'))
447 for port in self.ims_conf.get(
448 vnfr.get('name')).get('ports'):
449 if servertest(floatingIp.get('ip'), port):
451 "VNFC instance %s is reachable at %s:%s",
452 vnfci.get('hostname'),
453 floatingIp.get('ip'),
457 "VNFC instance %s is not reachable "
459 vnfci.get('hostname'),
460 floatingIp.get('ip'),
462 self.details["test_vnf"] = {
463 'status': "FAIL", 'result': (
464 "Port %s of server %s -> %s is "
467 vnfci.get('hostname'),
468 floatingIp.get('ip'))}
469 self.logger.error("Test VNF: ERROR")
472 self.details["test_vnf"] = {
474 'result': "All tests have been executed successfully"}
475 self.logger.info("Test VNF: OK")
479 self.main_agent.get_agent(
481 project_id=self.ob_projectid).delete(self.ob_nsr_id)
483 os_utils.delete_instance(nova_client=os_utils.get_nova_client(),
484 instance_id=self.ob_instance_id)
485 # question is the clean removing also the VM?
486 # I think so since is goinf to remove the tenant...
487 super(ImsVnf, self).clean()