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
10 """Orchestra OpenIMS testcase implementation."""
20 from snaps.config.flavor import FlavorConfig
21 from snaps.config.image import ImageConfig
22 from snaps.config.network import NetworkConfig, PortConfig, SubnetConfig
23 from snaps.config.router import RouterConfig
24 from snaps.config.security_group import (
25 Direction, Protocol, SecurityGroupConfig, SecurityGroupRuleConfig)
26 from snaps.config.vm_inst import VmInstanceConfig
28 from snaps.openstack.create_image import OpenStackImage
29 from snaps.openstack.create_flavor import OpenStackFlavor
30 from snaps.openstack.create_security_group import OpenStackSecurityGroup
31 from snaps.openstack.create_network import OpenStackNetwork
32 from snaps.openstack.create_router import OpenStackRouter
33 from snaps.openstack.create_instance import OpenStackVmInstance
35 from functest.opnfv_tests.openstack.snaps import snaps_utils
37 import functest.core.vnf as vnf
38 import functest.utils.openstack_utils as os_utils
39 from functest.utils.constants import CONST
41 from org.openbaton.cli.errors.errors import NfvoException
42 from org.openbaton.cli.agents.agents import MainAgent
45 __author__ = "Pauls, Michael <michael.pauls@fokus.fraunhofer.de>"
46 # ----------------------------------------------------------
50 # -----------------------------------------------------------
53 def get_config(parameter, file_path):
57 Returns the value of a given parameter in file.yaml
58 parameter must be given in string format with dots
59 Example: general.openstack.image_name
61 with open(file_path) as config_file:
62 file_yaml = yaml.safe_load(config_file)
65 for element in parameter.split("."):
66 value = value.get(element)
68 raise ValueError("The parameter %s is not defined in"
69 " reporting.yaml", parameter)
73 def servertest(host, port):
74 """Method to test that a server is reachable at IP:port"""
75 args = socket.getaddrinfo(host, port, socket.AF_INET, socket.SOCK_STREAM)
76 for family, socktype, proto, canonname, sockaddr in args:
77 sock = socket.socket(family, socktype, proto)
79 sock.connect(sockaddr)
87 def get_userdata(orchestrator=dict):
88 """Build userdata for Open Baton machine"""
89 userdata = "#!/bin/bash\n"
90 userdata += "echo \"Executing userdata...\"\n"
91 userdata += "set -x\n"
92 userdata += "set -e\n"
93 userdata += "echo \"Set nameserver to '8.8.8.8'...\"\n"
94 userdata += "echo \"nameserver 8.8.8.8\" >> /etc/resolv.conf\n"
95 userdata += "echo \"Install curl...\"\n"
96 userdata += "apt-get install curl\n"
97 userdata += "echo \"Inject public key...\"\n"
98 userdata += ("echo \"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCuPXrV3"
99 "geeHc6QUdyUr/1Z+yQiqLcOskiEGBiXr4z76MK4abiFmDZ18OMQlc"
100 "fl0p3kS0WynVgyaOHwZkgy/DIoIplONVr2CKBKHtPK+Qcme2PVnCtv"
101 "EqItl/FcD+1h5XSQGoa+A1TSGgCod/DPo+pes0piLVXP8Ph6QS1k7S"
102 "ic7JDeRQ4oT1bXYpJ2eWBDMfxIWKZqcZRiGPgMIbJ1iEkxbpeaAd9O"
103 "4MiM9nGCPESmed+p54uYFjwEDlAJZShcAZziiZYAvMZhvAhe6USljc"
104 "7YAdalAnyD/jwCHuwIrUw/lxo7UdNCmaUxeobEYyyFA1YVXzpNFZya"
105 "XPGAAYIJwEq/ openbaton@opnfv\" >> /home/ubuntu/.ssh/aut"
107 userdata += "echo \"Download bootstrap...\"\n"
108 userdata += ("curl -s %s "
109 "> ./bootstrap\n" % orchestrator['bootstrap']['url'])
110 userdata += ("curl -s %s" "> ./config_file\n" %
111 orchestrator['bootstrap']['config']['url'])
112 userdata += ("echo \"Disable usage of mysql...\"\n")
113 userdata += "sed -i s/mysql=.*/mysql=no/g /config_file\n"
114 userdata += ("echo \"Setting 'rabbitmq_broker_ip' to '%s'\"\n"
115 % orchestrator['details']['fip'].ip)
116 userdata += ("sed -i s/rabbitmq_broker_ip=localhost/rabbitmq_broker_ip"
117 "=%s/g /config_file\n" % orchestrator['details']['fip'].ip)
118 userdata += "echo \"Set autostart of components to 'false'\"\n"
119 userdata += "export OPENBATON_COMPONENT_AUTOSTART=false\n"
120 userdata += "echo \"Execute bootstrap...\"\n"
121 bootstrap = "sh ./bootstrap release -configFile=./config_file"
122 userdata += bootstrap + "\n"
123 userdata += "echo \"Setting 'nfvo.plugin.timeout' to '300000'\"\n"
124 userdata += ("echo \"nfvo.plugin.timeout=600000\" >> "
125 "/etc/openbaton/openbaton-nfvo.properties\n")
127 "wget %s -O /etc/openbaton/openbaton-vnfm-generic-user-data.sh\n" %
128 orchestrator['gvnfm']['userdata']['url'])
129 userdata += "sed -i '113i"'\ \ \ \ '"sleep 60' " \
130 "/etc/openbaton/openbaton-vnfm-generic-user-data.sh\n"
131 userdata += ("sed -i s/nfvo.marketplace.port=8082/nfvo.marketplace."
132 "port=8080/g /etc/openbaton/openbaton-nfvo.properties\n")
133 userdata += "echo \"Starting NFVO\"\n"
134 userdata += "service openbaton-nfvo restart\n"
135 userdata += "echo \"Starting Generic VNFM\"\n"
136 userdata += "service openbaton-vnfm-generic restart\n"
137 userdata += "echo \"...end of userdata...\"\n"
141 class OpenImsVnf(vnf.VnfOnBoarding):
142 """OpenIMS VNF deployed with openBaton orchestrator"""
144 # logger = logging.getLogger(__name__)
146 def __init__(self, **kwargs):
147 if "case_name" not in kwargs:
148 kwargs["case_name"] = "orchestra_openims"
149 super(OpenImsVnf, self).__init__(**kwargs)
150 self.logger = logging.getLogger("functest.ci.run_tests.orchestra")
151 self.logger.info("kwargs %s", (kwargs))
153 self.case_dir = pkg_resources.resource_filename(
154 'functest', 'opnfv_tests/vnf/ims/')
155 self.data_dir = CONST.__getattribute__('dir_ims_data')
156 self.test_dir = CONST.__getattribute__('dir_repo_vims_test')
157 self.created_resources = []
158 self.logger.info("%s VNF onboarding test starting", self.case_name)
161 self.config = CONST.__getattribute__(
162 'vnf_{}_config'.format(self.case_name))
163 except BaseException:
164 raise Exception("Orchestra VNF config file not found")
165 config_file = self.case_dir + self.config
168 get_config("mano", config_file),
171 self.logger.debug("Orchestrator configuration %s", self.mano)
173 self.details['orchestrator'] = dict(
174 name=self.mano['name'],
175 version=self.mano['version'],
181 get_config(self.case_name, config_file),
183 self.logger.debug("VNF configuration: %s", self.vnf)
185 self.details['vnf'] = dict(
186 name=self.vnf['name'],
189 self.details['test_vnf'] = dict(
193 # Orchestra base Data directory creation
194 if not os.path.exists(self.data_dir):
195 os.makedirs(self.data_dir)
197 self.images = get_config("tenant_images.orchestrator", config_file)
198 self.images.update(get_config("tenant_images.%s" %
199 self.case_name, config_file))
202 """Prepare testscase (Additional pre-configuration steps)."""
203 super(OpenImsVnf, self).prepare()
205 self.logger.info("Additional pre-configuration steps")
207 "tenant": self.tenant_name,
208 "username": self.tenant_name,
209 "password": self.tenant_name,
210 "auth_url": os_utils.get_credentials()['auth_url']
212 self.prepare_images()
213 self.prepare_flavor()
214 self.prepare_security_groups()
215 self.prepare_network()
216 self.prepare_floating_ip()
218 def prepare_images(self):
219 """Upload images if they doen't exist yet"""
220 self.logger.info("Upload images if they doen't exist yet")
221 for image_name, image_file in self.images.iteritems():
222 self.logger.info("image: %s, file: %s", image_name, image_file)
223 if image_file and image_name:
224 image = OpenStackImage(
226 ImageConfig(name=image_name,
229 image_file=image_file,
232 # self.created_resources.append(image);
234 def prepare_security_groups(self):
235 """Create Open Baton security group if it doesn't exist yet"""
237 "Creating security group for Open Baton if not yet existing...")
240 SecurityGroupRuleConfig(
241 sec_grp_name="orchestra-sec-group-allowall",
242 direction=Direction.ingress,
243 protocol=Protocol.tcp,
245 port_range_max=65535))
247 SecurityGroupRuleConfig(
248 sec_grp_name="orchestra-sec-group-allowall",
249 direction=Direction.egress,
250 protocol=Protocol.tcp,
252 port_range_max=65535))
254 SecurityGroupRuleConfig(
255 sec_grp_name="orchestra-sec-group-allowall",
256 direction=Direction.ingress,
257 protocol=Protocol.udp,
259 port_range_max=65535))
261 SecurityGroupRuleConfig(
262 sec_grp_name="orchestra-sec-group-allowall",
263 direction=Direction.egress,
264 protocol=Protocol.udp,
266 port_range_max=65535))
267 security_group = OpenStackSecurityGroup(
270 name="orchestra-sec-group-allowall",
271 rule_settings=sg_rules))
273 security_group_info = security_group.create()
274 self.created_resources.append(security_group)
275 self.mano['details']['sec_group'] = security_group_info.name
277 "Security group orchestra-sec-group-allowall prepared")
279 def prepare_flavor(self):
280 """Create Open Baton flavor if it doesn't exist yet"""
282 "Create Flavor for Open Baton NFVO if not yet existing")
284 flavor_settings = FlavorConfig(
285 name=self.mano['requirements']['flavor']['name'],
286 ram=self.mano['requirements']['flavor']['ram_min'],
287 disk=self.mano['requirements']['flavor']['disk'],
288 vcpus=self.mano['requirements']['flavor']['vcpus'])
289 flavor = OpenStackFlavor(self.snaps_creds, flavor_settings)
290 flavor_info = flavor.create()
291 self.created_resources.append(flavor)
292 self.mano['details']['flavor'] = {}
293 self.mano['details']['flavor']['name'] = flavor_settings.name
294 self.mano['details']['flavor']['id'] = flavor_info.id
296 def prepare_network(self):
297 """Create network/subnet/router if they doen't exist yet"""
299 "Creating network/subnet/router if they doen't exist yet...")
300 subnet_settings = SubnetConfig(
303 cidr="192.168.100.0/24")
304 network_settings = NetworkConfig(
307 subnet_settings=[subnet_settings])
308 orchestra_network = OpenStackNetwork(
309 self.snaps_creds, network_settings)
310 orchestra_network_info = orchestra_network.create()
311 self.mano['details']['network'] = {}
312 self.mano['details']['network']['id'] = orchestra_network_info.id
313 self.mano['details']['network']['name'] = orchestra_network_info.name
314 self.mano['details']['external_net_name'] = \
315 snaps_utils.get_ext_net_name(self.snaps_creds)
316 self.created_resources.append(orchestra_network)
317 orchestra_router = OpenStackRouter(
322 external_gateway=self.mano['details']['external_net_name'],
324 subnet_settings.name]))
325 orchestra_router.create()
326 self.created_resources.append(orchestra_router)
327 self.logger.info("Created network and router for Open Baton NFVO...")
329 def prepare_floating_ip(self):
330 """Select/Create Floating IP if it doesn't exist yet"""
331 self.logger.info("Retrieving floating IP for Open Baton NFVO")
332 neutron_client = snaps_utils.neutron_utils.neutron_client(
334 # Finding Tenant ID to check to which tenant the Floating IP belongs
335 tenant_id = os_utils.get_tenant_id(
336 os_utils.get_keystone_client(self.creds),
338 # Use os_utils to retrieve complete information of Floating IPs
339 floating_ips = os_utils.get_floating_ips(neutron_client)
341 # Filter Floating IPs with tenant id
342 for floating_ip in floating_ips:
343 # self.logger.info("Floating IP: %s", floating_ip)
344 if floating_ip.get('tenant_id') == tenant_id:
345 my_floating_ips.append(floating_ip.get('floating_ip_address'))
346 # Select if Floating IP exist else create new one
347 if len(my_floating_ips) >= 1:
348 # Get Floating IP object from snaps for clean up
349 snaps_floating_ips = snaps_utils.neutron_utils.get_floating_ips(
351 for my_floating_ip in my_floating_ips:
352 for snaps_floating_ip in snaps_floating_ips:
353 if snaps_floating_ip.ip == my_floating_ip:
354 self.mano['details']['fip'] = snaps_floating_ip
356 "Selected floating IP for Open Baton NFVO %s",
357 (self.mano['details']['fip'].ip))
359 if self.mano['details']['fip'] is not None:
362 self.logger.info("Creating floating IP for Open Baton NFVO")
363 self.mano['details']['fip'] = (
364 snaps_utils.neutron_utils. create_floating_ip(
365 neutron_client, self.mano['details']['external_net_name']))
367 "Created floating IP for Open Baton NFVO %s",
368 (self.mano['details']['fip'].ip))
370 def get_vim_descriptor(self):
371 """"Create VIM descriptor to be used for onboarding"""
373 "Building VIM descriptor with PoP creds: %s",
375 # Depending on API version either tenant ID or project name must be
377 if os_utils.is_keystone_v3():
379 "Using v3 API of OpenStack... -> Using OS_PROJECT_ID")
380 project_id = os_utils.get_tenant_id(
381 os_utils.get_keystone_client(),
382 self.creds.get("project_name"))
385 "Using v2 API of OpenStack... -> Using OS_TENANT_NAME")
386 project_id = self.creds.get("tenant_name")
387 self.logger.debug("VIM project/tenant id: %s", project_id)
389 "name": "vim-instance",
390 "authUrl": self.creds.get("auth_url"),
391 "tenant": project_id,
392 "username": self.creds.get("username"),
393 "password": self.creds.get("password"),
395 self.mano['details']['sec_group']
400 "latitude": "52.525876",
401 "longitude": "13.314400"
404 self.logger.info("Built VIM descriptor: %s", vim_json)
407 def deploy_orchestrator(self):
408 self.logger.info("Deploying Open Baton...")
409 self.logger.info("Details: %s", self.mano['details'])
410 start_time = time.time()
412 self.logger.info("Creating orchestra instance...")
413 userdata = get_userdata(self.mano)
414 self.logger.info("flavor: %s\n"
417 self.mano['details']['flavor']['name'],
418 self.mano['requirements']['image'],
419 self.mano['details']['network']['id'])
420 self.logger.debug("userdata: %s\n", userdata)
422 image_settings = ImageConfig(
423 name=self.mano['requirements']['image'],
427 port_settings = PortConfig(
428 name='%s_port' % self.case_name,
429 network_name=self.mano['details']['network']['name'])
430 # build configuration of vm
431 orchestra_settings = VmInstanceConfig(
433 flavor=self.mano['details']['flavor']['name'],
434 port_settings=[port_settings],
435 security_group_names=[self.mano['details']['sec_group']],
436 userdata=str(userdata))
437 orchestra_vm = OpenStackVmInstance(self.snaps_creds,
441 orchestra_vm.create()
442 self.created_resources.append(orchestra_vm)
443 self.mano['details']['id'] = orchestra_vm.get_vm_info()['id']
445 "Created orchestra instance: %s",
446 self.mano['details']['id'])
448 self.logger.info("Associating floating ip: '%s' to VM '%s' ",
449 self.mano['details']['fip'].ip,
451 nova_client = os_utils.get_nova_client()
452 if not os_utils.add_floating_ip(
454 self.mano['details']['id'],
455 self.mano['details']['fip'].ip):
456 duration = time.time() - start_time
457 self.details["orchestrator"].update(
458 status='FAIL', duration=duration)
459 self.logger.error("Cannot associate floating IP to VM.")
462 self.logger.info("Waiting for Open Baton NFVO to be up and running...")
466 self.mano['details']['fip'].ip,
470 self.logger.info("Open Baton NFVO is not started yet (%ss)",
476 duration = time.time() - start_time
477 self.details["orchestrator"].update(
478 status='FAIL', duration=duration)
479 self.logger.error("Open Baton is not started correctly")
482 self.logger.info("Waiting for all components to be up and running...")
484 duration = time.time() - start_time
485 self.details["orchestrator"].update(status='PASS', duration=duration)
486 self.logger.info("Deploy Open Baton NFVO: OK")
489 def deploy_vnf(self):
490 start_time = time.time()
491 self.logger.info("Deploying %s...", self.vnf['name'])
493 main_agent = MainAgent(
494 nfvo_ip=self.mano['details']['fip'].ip,
498 username=self.mano['credentials']['username'],
499 password=self.mano['credentials']['password'])
502 "Create %s Flavor if not existing", self.vnf['name'])
503 flavor_settings = FlavorConfig(
504 name=self.vnf['requirements']['flavor']['name'],
505 ram=self.vnf['requirements']['flavor']['ram_min'],
506 disk=self.vnf['requirements']['flavor']['disk'],
507 vcpus=self.vnf['requirements']['flavor']['vcpus'])
508 flavor = OpenStackFlavor(self.snaps_creds, flavor_settings)
509 flavor_info = flavor.create()
510 self.logger.debug("Flavor id: %s", flavor_info.id)
512 self.logger.info("Getting project 'default'...")
513 project_agent = main_agent.get_agent("project", "")
514 for project in json.loads(project_agent.find()):
515 if project.get("name") == "default":
516 self.mano['details']['project_id'] = project.get("id")
517 self.logger.info("Found project 'default': %s", project)
520 vim_json = self.get_vim_descriptor()
521 self.logger.info("Registering VIM: %s", vim_json)
523 main_agent.get_agent(
524 "vim", project_id=self.mano['details']['project_id']).create(
525 entity=json.dumps(vim_json))
527 market_agent = main_agent.get_agent(
528 "market", project_id=self.mano['details']['project_id'])
531 self.logger.info("sending: %s", self.vnf['descriptor']['url'])
532 nsd = market_agent.create(entity=self.vnf['descriptor']['url'])
533 if nsd.get('id') is None:
534 self.logger.error("NSD not onboarded correctly")
535 duration = time.time() - start_time
536 self.details["vnf"].update(status='FAIL', duration=duration)
538 self.mano['details']['nsd_id'] = nsd.get('id')
539 self.logger.info("Onboarded NSD: " + nsd.get("name"))
541 nsr_agent = main_agent.get_agent(
542 "nsr", project_id=self.mano['details']['project_id'])
544 self.mano['details']['nsr'] = nsr_agent.create(
545 self.mano['details']['nsd_id'])
546 except NfvoException as exc:
547 self.logger.error(exc.message)
548 duration = time.time() - start_time
549 self.details["vnf"].update(status='FAIL', duration=duration)
552 if self.mano['details']['nsr'].get('code') is not None:
554 "%s cannot be deployed: %s -> %s",
556 self.mano['details']['nsr'].get('code'),
557 self.mano['details']['nsr'].get('message'))
558 self.logger.error("%s cannot be deployed", self.vnf['name'])
559 duration = time.time() - start_time
560 self.details["vnf"].update(status='FAIL', duration=duration)
564 self.logger.info("Waiting for NSR to go to ACTIVE...")
565 while self.mano['details']['nsr'].get("status") != 'ACTIVE' \
566 and self.mano['details']['nsr'].get("status") != 'ERROR':
568 self.logger.info("NSR is not yet ACTIVE... (%ss)", 60 * timeout)
570 self.logger.error("INACTIVE NSR after %s sec..", 60 * timeout)
571 duration = time.time() - start_time
572 self.details["vnf"].update(status='FAIL', duration=duration)
575 self.mano['details']['nsr'] = json.loads(
576 nsr_agent.find(self.mano['details']['nsr'].get('id')))
578 duration = time.time() - start_time
579 if self.mano['details']['nsr'].get("status") == 'ACTIVE':
580 self.details["vnf"].update(status='PASS', duration=duration)
581 self.logger.info("Sleep for 60s to ensure that all "
582 "services are up and running...")
586 self.details["vnf"].update(status='FAIL', duration=duration)
587 self.logger.error("NSR: %s", self.mano['details'].get('nsr'))
592 self.logger.info("Testing VNF OpenIMS...")
593 start_time = time.time()
595 "Testing if %s works properly...",
596 self.mano['details']['nsr'].get('name'))
597 for vnfr in self.mano['details']['nsr'].get('vnfr'):
599 "Checking ports %s of VNF %s",
600 self.vnf['test'][vnfr.get('name')]['ports'],
602 for vdu in vnfr.get('vdu'):
603 for vnfci in vdu.get('vnfc_instance'):
605 "Checking ports of VNFC instance %s",
606 vnfci.get('hostname'))
607 for floating_ip in vnfci.get('floatingIps'):
610 vnfci.get('hostname'),
611 floating_ip.get('ip'))
612 for port in self.vnf['test'][vnfr.get(
614 if servertest(floating_ip.get('ip'), port):
616 "VNFC instance %s is reachable at %s:%s",
617 vnfci.get('hostname'),
618 floating_ip.get('ip'),
622 "VNFC instance %s is not reachable "
624 vnfci.get('hostname'),
625 floating_ip.get('ip'),
627 duration = time.time() - start_time
628 self.details["test_vnf"].update(
629 status='FAIL', duration=duration, esult=(
630 "Port %s of server %s -> %s is "
633 vnfci.get('hostname'),
634 floating_ip.get('ip')))
635 self.logger.error("Test VNF: ERROR")
637 duration = time.time() - start_time
638 self.details["test_vnf"].update(status='PASS', duration=duration)
639 self.logger.info("Test VNF: OK")
643 self.logger.info("Cleaning %s...", self.case_name)
645 main_agent = MainAgent(
646 nfvo_ip=self.mano['details']['fip'].ip,
650 username=self.mano['credentials']['username'],
651 password=self.mano['credentials']['password'])
652 self.logger.info("Terminating %s...", self.vnf['name'])
653 if (self.mano['details'].get('nsr')):
654 main_agent.get_agent(
656 project_id=self.mano['details']['project_id']).\
657 delete(self.mano['details']['nsr'].get('id'))
658 self.logger.info("Sleeping 60 seconds...")
661 self.logger.info("No need to terminate the VNF...")
662 # os_utils.delete_instance(nova_client=os_utils.get_nova_client(),
663 # instance_id=self.mano_instance_id)
664 except (NfvoException, KeyError) as exc:
665 self.logger.error('Unexpected error cleaning - %s', exc)
668 neutron_client = os_utils.get_neutron_client(self.creds)
669 self.logger.info("Deleting Open Baton Port...")
670 port = snaps_utils.neutron_utils.get_port(
672 port_name='%s_port' % self.case_name)
673 snaps_utils.neutron_utils.delete_port(neutron_client, port)
675 except Exception as exc:
676 self.logger.error('Unexpected error cleaning - %s', exc)
678 self.logger.info("Deleting Open Baton Floating IP...")
679 snaps_utils.neutron_utils.delete_floating_ip(
680 neutron_client, self.mano['details']['fip'])
681 except Exception as exc:
682 self.logger.error('Unexpected error cleaning - %s', exc)
684 for resource in reversed(self.created_resources):
686 self.logger.info("Cleaning %s", str(resource))
688 except Exception as exc:
689 self.logger.error('Unexpected error cleaning - %s', exc)
690 super(OpenImsVnf, self).clean()