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."""
21 from snaps.openstack.create_image import OpenStackImage, ImageSettings
22 from snaps.openstack.create_flavor import OpenStackFlavor, FlavorSettings
23 from snaps.openstack.create_security_group import (
24 OpenStackSecurityGroup,
25 SecurityGroupSettings,
26 SecurityGroupRuleSettings,
29 from snaps.openstack.create_network import (
34 from snaps.openstack.create_router import OpenStackRouter, RouterSettings
35 from snaps.openstack.os_credentials import OSCreds
36 from snaps.openstack.create_instance import (
37 VmInstanceSettings, OpenStackVmInstance)
38 from functest.opnfv_tests.openstack.snaps import snaps_utils
40 import functest.core.vnf as vnf
41 import functest.utils.openstack_utils as os_utils
42 from functest.utils.constants import CONST
44 from org.openbaton.cli.errors.errors import NfvoException
45 from org.openbaton.cli.agents.agents import MainAgent
48 __author__ = "Pauls, Michael <michael.pauls@fokus.fraunhofer.de>"
49 # ----------------------------------------------------------
53 # -----------------------------------------------------------
56 def get_config(parameter, file_path):
60 Returns the value of a given parameter in file.yaml
61 parameter must be given in string format with dots
62 Example: general.openstack.image_name
64 with open(file_path) as config_file:
65 file_yaml = yaml.safe_load(config_file)
68 for element in parameter.split("."):
69 value = value.get(element)
71 raise ValueError("The parameter %s is not defined in"
72 " reporting.yaml", parameter)
76 def servertest(host, port):
77 """Method to test that a server is reachable at IP:port"""
78 args = socket.getaddrinfo(host, port, socket.AF_INET, socket.SOCK_STREAM)
79 for family, socktype, proto, canonname, sockaddr in args:
80 sock = socket.socket(family, socktype, proto)
82 sock.connect(sockaddr)
90 def get_userdata(orchestrator=dict):
91 """Build userdata for Open Baton machine"""
92 userdata = "#!/bin/bash\n"
93 userdata += "echo \"Executing userdata...\"\n"
94 userdata += "set -x\n"
95 userdata += "set -e\n"
96 userdata += "echo \"Set nameserver to '8.8.8.8'...\"\n"
97 userdata += "echo \"nameserver 8.8.8.8\" >> /etc/resolv.conf\n"
98 userdata += "echo \"Install curl...\"\n"
99 userdata += "apt-get install curl\n"
100 userdata += "echo \"Inject public key...\"\n"
101 userdata += ("echo \"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCuPXrV3"
102 "geeHc6QUdyUr/1Z+yQiqLcOskiEGBiXr4z76MK4abiFmDZ18OMQlc"
103 "fl0p3kS0WynVgyaOHwZkgy/DIoIplONVr2CKBKHtPK+Qcme2PVnCtv"
104 "EqItl/FcD+1h5XSQGoa+A1TSGgCod/DPo+pes0piLVXP8Ph6QS1k7S"
105 "ic7JDeRQ4oT1bXYpJ2eWBDMfxIWKZqcZRiGPgMIbJ1iEkxbpeaAd9O"
106 "4MiM9nGCPESmed+p54uYFjwEDlAJZShcAZziiZYAvMZhvAhe6USljc"
107 "7YAdalAnyD/jwCHuwIrUw/lxo7UdNCmaUxeobEYyyFA1YVXzpNFZya"
108 "XPGAAYIJwEq/ openbaton@opnfv\" >> /home/ubuntu/.ssh/aut"
110 userdata += "echo \"Download bootstrap...\"\n"
111 userdata += ("curl -s %s "
112 "> ./bootstrap\n" % orchestrator['bootstrap']['url'])
113 userdata += ("curl -s %s" "> ./config_file\n" %
114 orchestrator['bootstrap']['config']['url'])
115 userdata += ("echo \"Disable usage of mysql...\"\n")
116 userdata += "sed -i s/mysql=.*/mysql=no/g /config_file\n"
117 userdata += ("echo \"Setting 'rabbitmq_broker_ip' to '%s'\"\n"
118 % orchestrator['details']['fip'].ip)
119 userdata += ("sed -i s/rabbitmq_broker_ip=localhost/rabbitmq_broker_ip"
120 "=%s/g /config_file\n" % orchestrator['details']['fip'].ip)
121 userdata += "echo \"Set autostart of components to 'false'\"\n"
122 userdata += "export OPENBATON_COMPONENT_AUTOSTART=false\n"
123 userdata += "echo \"Execute bootstrap...\"\n"
124 bootstrap = "sh ./bootstrap release -configFile=./config_file"
125 userdata += bootstrap + "\n"
126 userdata += "echo \"Setting 'nfvo.plugin.timeout' to '300000'\"\n"
127 userdata += ("echo \"nfvo.plugin.timeout=600000\" >> "
128 "/etc/openbaton/openbaton-nfvo.properties\n")
130 "wget %s -O /etc/openbaton/openbaton-vnfm-generic-user-data.sh\n" %
131 orchestrator['gvnfm']['userdata']['url'])
132 userdata += "sed -i '113i"'\ \ \ \ '"sleep 60' " \
133 "/etc/openbaton/openbaton-vnfm-generic-user-data.sh\n"
134 userdata += "echo \"Starting NFVO\"\n"
135 userdata += "service openbaton-nfvo restart\n"
136 userdata += "echo \"Starting Generic VNFM\"\n"
137 userdata += "service openbaton-vnfm-generic restart\n"
138 userdata += "echo \"...end of userdata...\"\n"
142 class ImsVnf(vnf.VnfOnBoarding):
143 """OpenIMS VNF deployed with openBaton orchestrator"""
145 # logger = logging.getLogger(__name__)
147 def __init__(self, **kwargs):
148 if "case_name" not in kwargs:
149 kwargs["case_name"] = "orchestra_ims"
150 super(ImsVnf, self).__init__(**kwargs)
151 self.logger = logging.getLogger("functest.ci.run_tests.orchestra")
152 self.logger.info("kwargs %s", (kwargs))
154 self.case_dir = pkg_resources.resource_filename(
155 'functest', 'opnfv_tests/vnf/ims/')
156 self.data_dir = CONST.__getattribute__('dir_ims_data')
157 self.test_dir = CONST.__getattribute__('dir_repo_vims_test')
158 self.created_resources = []
159 self.logger.info("Orchestra IMS VNF onboarding test starting")
162 self.config = CONST.__getattribute__(
163 'vnf_{}_config'.format(self.case_name))
164 except BaseException:
165 raise Exception("Orchestra VNF config file not found")
166 config_file = self.case_dir + self.config
169 requirements=get_config("orchestrator.requirements", config_file),
170 credentials=get_config("orchestrator.credentials", config_file),
171 bootstrap=get_config("orchestrator.bootstrap", config_file),
172 gvnfm=get_config("orchestrator.gvnfm", config_file),
174 self.logger.debug("Orchestrator configuration %s", self.baton)
176 self.details['orchestrator'] = dict(
177 name=get_config("orchestrator.name", config_file),
178 version=get_config("orchestrator.version", config_file),
182 self.baton['details'] = {}
183 self.baton['details']['image'] = self.baton['requirements']['os_image']
184 self.baton['details']['name'] = self.details['orchestrator']['name']
187 descriptor=get_config("vnf.descriptor", config_file),
188 requirements=get_config("vnf.requirements", config_file)
190 self.details['vnf'] = dict(
191 name=get_config("vnf.name", config_file),
193 self.logger.debug("VNF configuration: %s", self.vnf)
195 self.details['test_vnf'] = dict(
199 # vIMS Data directory creation
200 if not os.path.exists(self.data_dir):
201 os.makedirs(self.data_dir)
203 self.images = get_config("tenant_images", config_file)
204 self.ims_conf = get_config("vIMS", config_file)
205 self.snaps_creds = None
208 """Prepare testscase (Additional pre-configuration steps)."""
209 super(ImsVnf, self).prepare()
211 self.logger.info("Additional pre-configuration steps")
212 self.logger.info("creds %s", (self.creds))
214 self.snaps_creds = OSCreds(
215 username=self.creds['username'],
216 password=self.creds['password'],
217 auth_url=self.creds['auth_url'],
218 project_name=self.creds['tenant'],
219 identity_api_version=int(os_utils.get_keystone_client_version()))
221 self.prepare_images()
222 self.prepare_flavor()
223 self.prepare_security_groups()
224 self.prepare_network()
225 self.prepare_floating_ip()
227 def prepare_images(self):
228 """Upload images if they doen't exist yet"""
229 self.logger.info("Upload images if they doen't exist yet")
230 for image_name, image_url in self.images.iteritems():
231 self.logger.info("image: %s, url: %s", image_name, image_url)
232 if image_url and image_name:
233 image = OpenStackImage(
235 ImageSettings(name=image_name,
240 # self.created_resources.append(image);
242 def prepare_security_groups(self):
243 """Create Open Baton security group if it doesn't exist yet"""
245 "Creating security group for Open Baton if not yet existing...")
248 SecurityGroupRuleSettings(
249 sec_grp_name="orchestra-sec-group-allowall",
250 direction=Direction.ingress,
251 protocol=Protocol.tcp,
253 port_range_max=65535))
255 SecurityGroupRuleSettings(
256 sec_grp_name="orchestra-sec-group-allowall",
257 direction=Direction.egress,
258 protocol=Protocol.tcp,
260 port_range_max=65535))
262 SecurityGroupRuleSettings(
263 sec_grp_name="orchestra-sec-group-allowall",
264 direction=Direction.ingress,
265 protocol=Protocol.udp,
267 port_range_max=65535))
269 SecurityGroupRuleSettings(
270 sec_grp_name="orchestra-sec-group-allowall",
271 direction=Direction.egress,
272 protocol=Protocol.udp,
274 port_range_max=65535))
276 # SecurityGroupRuleSettings(
277 # sec_grp_name="orchestra-sec-group-allowall",
278 # direction=Direction.ingress,
279 # protocol=Protocol.icmp,
281 # port_range_max=-1))
283 # SecurityGroupRuleSettings(
284 # sec_grp_name="orchestra-sec-group-allowall",
285 # direction=Direction.egress,
286 # protocol=Protocol.icmp,
288 # port_range_max=-1))
290 security_group = OpenStackSecurityGroup(
292 SecurityGroupSettings(
293 name="orchestra-sec-group-allowall",
294 rule_settings=sg_rules))
296 security_group_info = security_group.create()
297 self.created_resources.append(security_group)
298 self.baton['details']['sec_group'] = security_group_info.name
300 "Security group orchestra-sec-group-allowall prepared")
302 def prepare_flavor(self):
303 """Create Open Baton flavor if it doesn't exist yet"""
305 "Create Flavor for Open Baton NFVO if not yet existing")
307 flavor_settings = FlavorSettings(
308 name=self.baton['requirements']['flavor']['name'],
309 ram=self.baton['requirements']['flavor']['ram_min'],
310 disk=self.baton['requirements']['flavor']['disk'],
311 vcpus=self.baton['requirements']['flavor']['vcpus'])
312 flavor = OpenStackFlavor(self.snaps_creds, flavor_settings)
313 flavor_info = flavor.create()
314 self.created_resources.append(flavor)
315 self.baton['details']['flavor'] = {}
316 self.baton['details']['flavor']['name'] = flavor_settings.name
317 self.baton['details']['flavor']['id'] = flavor_info.id
319 def prepare_network(self):
320 """Create network/subnet/router if they doen't exist yet"""
322 "Creating network/subnet/router if they doen't exist yet...")
323 subnet_settings = SubnetSettings(
325 self.baton['details']['name'],
326 cidr="192.168.100.0/24")
327 network_settings = NetworkSettings(
329 self.baton['details']['name'],
330 subnet_settings=[subnet_settings])
331 orchestra_network = OpenStackNetwork(
332 self.snaps_creds, network_settings)
333 orchestra_network_info = orchestra_network.create()
334 self.baton['details']['network'] = {}
335 self.baton['details']['network']['id'] = orchestra_network_info.id
336 self.baton['details']['network']['name'] = orchestra_network_info.name
337 self.baton['details']['external_net_name'] = \
338 snaps_utils.get_ext_net_name(self.snaps_creds)
339 self.created_resources.append(orchestra_network)
340 orchestra_router = OpenStackRouter(
344 self.baton['details']['name'],
345 external_gateway=self.baton['details']['external_net_name'],
347 subnet_settings.name]))
348 orchestra_router.create()
349 self.created_resources.append(orchestra_router)
350 self.logger.info("Created network and router for Open Baton NFVO...")
352 def prepare_floating_ip(self):
353 """Select/Create Floating IP if it doesn't exist yet"""
354 self.logger.info("Retrieving floating IP for Open Baton NFVO")
355 neutron_client = snaps_utils.neutron_utils.neutron_client(
357 # Finding Tenant ID to check to which tenant the Floating IP belongs
358 tenant_id = os_utils.get_tenant_id(
359 os_utils.get_keystone_client(self.creds),
361 # Use os_utils to retrieve complete information of Floating IPs
362 floating_ips = os_utils.get_floating_ips(neutron_client)
364 # Filter Floating IPs with tenant id
365 for floating_ip in floating_ips:
366 # self.logger.info("Floating IP: %s", floating_ip)
367 if floating_ip.get('tenant_id') == tenant_id:
368 my_floating_ips.append(floating_ip.get('floating_ip_address'))
369 # Select if Floating IP exist else create new one
370 if len(my_floating_ips) >= 1:
371 # Get Floating IP object from snaps for clean up
372 snaps_floating_ips = snaps_utils.neutron_utils.get_floating_ips(
374 for my_floating_ip in my_floating_ips:
375 for snaps_floating_ip in snaps_floating_ips:
376 if snaps_floating_ip.ip == my_floating_ip:
377 self.baton['details']['fip'] = snaps_floating_ip
379 "Selected floating IP for Open Baton NFVO %s",
380 (self.baton['details']['fip'].ip))
382 if self.baton['details']['fip'] is not None:
385 self.logger.info("Creating floating IP for Open Baton NFVO")
386 self.baton['details']['fip'] = \
387 snaps_utils.neutron_utils.create_floating_ip(
388 neutron_client, self.baton['details']['external_net_name'])
390 "Created floating IP for Open Baton NFVO %s",
391 (self.baton['details']['fip'].ip))
393 def get_vim_descriptor(self):
394 """"Create VIM descriptor to be used for onboarding"""
396 "Building VIM descriptor with PoP creds: %s",
398 # Depending on API version either tenant ID or project name must be
400 if os_utils.is_keystone_v3():
402 "Using v3 API of OpenStack... -> Using OS_PROJECT_ID")
403 project_id = os_utils.get_tenant_id(
404 os_utils.get_keystone_client(),
405 self.creds.get("project_name"))
408 "Using v2 API of OpenStack... -> Using OS_TENANT_NAME")
409 project_id = self.creds.get("tenant_name")
410 self.logger.debug("VIM project/tenant id: %s", project_id)
412 "name": "vim-instance",
413 "authUrl": self.creds.get("auth_url"),
414 "tenant": project_id,
415 "username": self.creds.get("username"),
416 "password": self.creds.get("password"),
418 self.baton['details']['sec_group']
423 "latitude": "52.525876",
424 "longitude": "13.314400"
427 self.logger.info("Built VIM descriptor: %s", vim_json)
430 def deploy_orchestrator(self):
431 self.logger.info("Deploying orchestrator Open Baton ...")
432 self.logger.info("Details: %s", self.baton['details'])
433 start_time = time.time()
435 self.logger.info("Creating orchestra instance...")
436 userdata = get_userdata(self.baton)
437 self.logger.info("flavor: %s\n"
440 self.baton['details']['flavor']['name'],
441 self.baton['details']['image'],
442 self.baton['details']['network']['id'])
443 self.logger.debug("userdata: %s\n", userdata)
445 image_settings = ImageSettings(
446 name=self.baton['details']['image'],
450 port_settings = PortSettings(
451 name='%s_port' % self.baton['details']['name'],
452 network_name=self.baton['details']['network']['name'])
453 # build configuration of vm
454 orchestra_settings = VmInstanceSettings(
455 name=self.baton['details']['name'],
456 flavor=self.baton['details']['flavor']['name'],
457 port_settings=[port_settings],
458 security_group_names=[self.baton['details']['sec_group']],
460 orchestra_vm = OpenStackVmInstance(self.snaps_creds,
464 orchestra_vm.create()
465 self.created_resources.append(orchestra_vm)
466 self.baton['details']['id'] = orchestra_vm.get_vm_info()['id']
468 "Created orchestra instance: %s",
469 self.baton['details']['id'])
471 self.logger.info("Associating floating ip: '%s' to VM '%s' ",
472 self.baton['details']['fip'].ip,
473 self.baton['details']['name'])
474 nova_client = os_utils.get_nova_client()
475 if not os_utils.add_floating_ip(
477 self.baton['details']['id'],
478 self.baton['details']['fip'].ip):
479 duration = time.time() - start_time
480 self.details["orchestrator"].update(
481 status='FAIL', duration=duration)
482 self.logger.error("Cannot associate floating IP to VM.")
485 self.logger.info("Waiting for Open Baton NFVO to be up and running...")
489 self.baton['details']['fip'].ip,
493 self.logger.info("Open Baton NFVO is not started yet (%ss)",
499 duration = time.time() - start_time
500 self.details["orchestrator"].update(
501 status='FAIL', duration=duration)
502 self.logger.error("Open Baton is not started correctly")
505 self.logger.info("Waiting for all components to be up and running...")
507 duration = time.time() - start_time
508 self.details["orchestrator"].update(status='PASS', duration=duration)
509 self.logger.info("Deploy Open Baton NFVO: OK")
512 def deploy_vnf(self):
513 start_time = time.time()
514 self.logger.info("Deploying OpenIMS...")
516 main_agent = MainAgent(
517 nfvo_ip=self.baton['details']['fip'].ip,
521 username=self.baton['credentials']['username'],
522 password=self.baton['credentials']['password'])
525 "Check if openims Flavor is available, if not, create one")
526 flavor_settings = FlavorSettings(
527 name=self.vnf['requirements']['flavor']['name'],
528 ram=self.vnf['requirements']['flavor']['ram_min'],
529 disk=self.vnf['requirements']['flavor']['disk'],
530 vcpus=self.vnf['requirements']['flavor']['vcpus'])
531 flavor = OpenStackFlavor(self.snaps_creds, flavor_settings)
532 flavor_info = flavor.create()
533 self.logger.debug("Flavor id: %s", flavor_info.id)
535 self.logger.info("Getting project 'default'...")
536 project_agent = main_agent.get_agent("project", "")
537 for project in json.loads(project_agent.find()):
538 if project.get("name") == "default":
539 self.baton['details']['project_id'] = project.get("id")
540 self.logger.info("Found project 'default': %s", project)
543 vim_json = self.get_vim_descriptor()
544 self.logger.info("Registering VIM: %s", vim_json)
546 main_agent.get_agent(
547 "vim", project_id=self.baton['details']['project_id']).create(
548 entity=json.dumps(vim_json))
550 market_agent = main_agent.get_agent(
551 "market", project_id=self.baton['details']['project_id'])
554 self.logger.info("sending: %s", self.vnf['descriptor']['url'])
555 nsd = market_agent.create(entity=self.vnf['descriptor']['url'])
556 if nsd.get('id') is None:
557 self.logger.error("NSD not onboarded correctly")
558 duration = time.time() - start_time
559 self.details["vnf"].update(status='FAIL', duration=duration)
561 self.baton['details']['nsd_id'] = nsd.get('id')
562 self.logger.info("Onboarded NSD: " + nsd.get("name"))
564 nsr_agent = main_agent.get_agent(
565 "nsr", project_id=self.baton['details']['project_id'])
567 self.baton['details']['nsr'] = nsr_agent.create(
568 self.baton['details']['nsd_id'])
569 except NfvoException as exc:
570 self.logger.error(exc.message)
571 duration = time.time() - start_time
572 self.details["vnf"].update(status='FAIL', duration=duration)
575 if self.baton['details']['nsr'].get('code') is not None:
577 "vIMS cannot be deployed: %s -> %s",
578 self.baton['details']['nsr'].get('code'),
579 self.baton['details']['nsr'].get('message'))
580 self.logger.error("vIMS cannot be deployed")
581 duration = time.time() - start_time
582 self.details["vnf"].update(status='FAIL', duration=duration)
586 self.logger.info("Waiting for NSR to go to ACTIVE...")
587 while self.baton['details']['nsr'].get("status") != 'ACTIVE' \
588 and self.baton['details']['nsr'].get("status") != 'ERROR':
590 self.logger.info("NSR is not yet ACTIVE... (%ss)", 5 * timeout)
592 self.logger.error("INACTIVE NSR after %s sec..", 5 * timeout)
593 duration = time.time() - start_time
594 self.details["vnf"].update(status='FAIL', duration=duration)
597 self.baton['details']['nsr'] = json.loads(
598 nsr_agent.find(self.baton['details']['nsr'].get('id')))
600 duration = time.time() - start_time
601 if self.baton['details']['nsr'].get("status") == 'ACTIVE':
602 self.details["vnf"].update(status='PASS', duration=duration)
603 self.logger.info("Sleep for 60s to ensure that all "
604 "services are up and running...")
608 self.details["vnf"].update(status='FAIL', duration=duration)
609 self.logger.error("NSR: %s", self.baton['details'].get('nsr'))
614 self.logger.info("Testing VNF OpenIMS...")
615 start_time = time.time()
617 "Testing if %s works properly...",
618 self.baton['details']['nsr'].get('name'))
619 for vnfr in self.baton['details']['nsr'].get('vnfr'):
621 "Checking ports %s of VNF %s",
622 self.ims_conf.get(vnfr.get('name')).get('ports'),
624 for vdu in vnfr.get('vdu'):
625 for vnfci in vdu.get('vnfc_instance'):
627 "Checking ports of VNFC instance %s",
628 vnfci.get('hostname'))
629 for floating_ip in vnfci.get('floatingIps'):
632 vnfci.get('hostname'),
633 floating_ip.get('ip'))
634 for port in self.ims_conf.get(
635 vnfr.get('name')).get('ports'):
636 if servertest(floating_ip.get('ip'), port):
638 "VNFC instance %s is reachable at %s:%s",
639 vnfci.get('hostname'),
640 floating_ip.get('ip'),
644 "VNFC instance %s is not reachable "
646 vnfci.get('hostname'),
647 floating_ip.get('ip'),
649 duration = time.time() - start_time
650 self.details["test_vnf"].update(
651 status='FAIL', duration=duration, esult=(
652 "Port %s of server %s -> %s is "
655 vnfci.get('hostname'),
656 floating_ip.get('ip')))
657 self.logger.error("Test VNF: ERROR")
659 duration = time.time() - start_time
660 self.details["test_vnf"].update(status='PASS', duration=duration)
661 self.logger.info("Test VNF: OK")
665 self.logger.info("Cleaning...")
667 main_agent = MainAgent(
668 nfvo_ip=self.baton['details']['fip'].ip,
672 username=self.baton['credentials']['username'],
673 password=self.baton['credentials']['password'])
674 self.logger.info("Terminating OpenIMS VNF...")
675 if (self.baton['details'].get('nsr')):
676 main_agent.get_agent(
678 project_id=self.baton['details']['project_id'])\
679 .delete(self.baton['details']['nsr'].get('id'))
680 self.logger.info("Waiting 60sec for terminating OpenIMS VNF..")
682 # os_utils.delete_instance(nova_client=os_utils.get_nova_client(),
683 # instance_id=self.baton_instance_id)
684 except (NfvoException, KeyError) as exc:
685 self.logger.error('Unexpected error cleaning - %s', exc)
688 neutron_client = os_utils.get_neutron_client(self.creds)
689 snaps_utils.neutron_utils.delete_floating_ip(
690 neutron_client, self.baton['details']['fip'])
691 except Exception as exc:
692 self.logger.error('Unexpected error cleaning - %s', exc)
694 for resource in reversed(self.created_resources):
696 self.logger.info("Cleaning %s", str(resource))
698 except Exception as exc:
699 self.logger.error('Unexpected error cleaning - %s', exc)
700 super(ImsVnf, self).clean()