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 Clearwater IMS testcase implementation."""
20 from snaps.openstack.create_image import OpenStackImage, ImageSettings
21 from snaps.openstack.create_flavor import OpenStackFlavor, FlavorSettings
22 from snaps.openstack.create_security_group import (
23 OpenStackSecurityGroup,
24 SecurityGroupSettings,
25 SecurityGroupRuleSettings,
28 from snaps.openstack.create_network import (
33 from snaps.openstack.create_router import OpenStackRouter, RouterSettings
34 from snaps.openstack.create_instance import (
37 from functest.opnfv_tests.openstack.snaps import snaps_utils
39 import functest.core.vnf as vnf
40 import functest.utils.openstack_utils as os_utils
41 from functest.utils.constants import CONST
43 from org.openbaton.cli.errors.errors import NfvoException
44 from org.openbaton.cli.agents.agents import MainAgent
47 __author__ = "Pauls, Michael <michael.pauls@fokus.fraunhofer.de>"
48 # ----------------------------------------------------------
52 # -----------------------------------------------------------
55 def get_config(parameter, file_path):
59 Returns the value of a given parameter in file.yaml
60 parameter must be given in string format with dots
61 Example: general.openstack.image_name
63 with open(file_path) as config_file:
64 file_yaml = yaml.safe_load(config_file)
67 for element in parameter.split("."):
68 value = value.get(element)
70 raise ValueError("The parameter %s is not defined in"
71 " reporting.yaml", parameter)
75 def servertest(host, port):
76 """Method to test that a server is reachable at IP:port"""
77 args = socket.getaddrinfo(host, port, socket.AF_INET, socket.SOCK_STREAM)
78 for family, socktype, proto, canonname, sockaddr in args:
79 sock = socket.socket(family, socktype, proto)
81 sock.connect(sockaddr)
89 def get_userdata(orchestrator=dict):
90 """Build userdata for Open Baton machine"""
91 userdata = "#!/bin/bash\n"
92 userdata += "echo \"Executing userdata...\"\n"
93 userdata += "set -x\n"
94 userdata += "set -e\n"
95 userdata += "echo \"Set nameserver to '8.8.8.8'...\"\n"
96 userdata += "echo \"nameserver 8.8.8.8\" >> /etc/resolv.conf\n"
97 userdata += "echo \"Install curl...\"\n"
98 userdata += "apt-get install curl\n"
99 userdata += "echo \"Inject public key...\"\n"
100 userdata += ("echo \"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCuPXrV3"
101 "geeHc6QUdyUr/1Z+yQiqLcOskiEGBiXr4z76MK4abiFmDZ18OMQlc"
102 "fl0p3kS0WynVgyaOHwZkgy/DIoIplONVr2CKBKHtPK+Qcme2PVnCtv"
103 "EqItl/FcD+1h5XSQGoa+A1TSGgCod/DPo+pes0piLVXP8Ph6QS1k7S"
104 "ic7JDeRQ4oT1bXYpJ2eWBDMfxIWKZqcZRiGPgMIbJ1iEkxbpeaAd9O"
105 "4MiM9nGCPESmed+p54uYFjwEDlAJZShcAZziiZYAvMZhvAhe6USljc"
106 "7YAdalAnyD/jwCHuwIrUw/lxo7UdNCmaUxeobEYyyFA1YVXzpNFZya"
107 "XPGAAYIJwEq/ openbaton@opnfv\" >> /home/ubuntu/.ssh/aut"
109 userdata += "echo \"Download bootstrap...\"\n"
110 userdata += ("curl -s %s "
111 "> ./bootstrap\n" % orchestrator['bootstrap']['url'])
112 userdata += ("curl -s %s" "> ./config_file\n" %
113 orchestrator['bootstrap']['config']['url'])
114 userdata += ("echo \"Disable usage of mysql...\"\n")
115 userdata += "sed -i s/mysql=.*/mysql=no/g /config_file\n"
116 userdata += ("echo \"Setting 'rabbitmq_broker_ip' to '%s'\"\n"
117 % orchestrator['details']['fip'].ip)
118 userdata += ("sed -i s/rabbitmq_broker_ip=localhost/rabbitmq_broker_ip"
119 "=%s/g /config_file\n" % orchestrator['details']['fip'].ip)
120 userdata += "echo \"Set autostart of components to 'false'\"\n"
121 userdata += "export OPENBATON_COMPONENT_AUTOSTART=false\n"
122 userdata += "echo \"Execute bootstrap...\"\n"
123 bootstrap = "sh ./bootstrap release -configFile=./config_file"
124 userdata += bootstrap + "\n"
125 userdata += "echo \"Setting 'nfvo.plugin.timeout' to '300000'\"\n"
126 userdata += ("echo \"nfvo.plugin.timeout=600000\" >> "
127 "/etc/openbaton/openbaton-nfvo.properties\n")
129 "wget %s -O /etc/openbaton/openbaton-vnfm-generic-user-data.sh\n" %
130 orchestrator['gvnfm']['userdata']['url'])
131 userdata += "sed -i '113i"'\ \ \ \ '"sleep 60' " \
132 "/etc/openbaton/openbaton-vnfm-generic-user-data.sh\n"
133 userdata += ("sed -i s/nfvo.marketplace.port=8082/nfvo.marketplace."
134 "port=8080/g /etc/openbaton/openbaton-nfvo.properties\n")
135 userdata += "echo \"Starting NFVO\"\n"
136 userdata += "service openbaton-nfvo restart\n"
137 userdata += "echo \"Starting Generic VNFM\"\n"
138 userdata += "service openbaton-vnfm-generic restart\n"
139 userdata += "echo \"...end of userdata...\"\n"
143 class ClearwaterImsVnf(vnf.VnfOnBoarding):
144 """Clearwater IMS VNF deployed with openBaton orchestrator"""
146 # logger = logging.getLogger(__name__)
148 def __init__(self, **kwargs):
149 if "case_name" not in kwargs:
150 kwargs["case_name"] = "orchestra_clearwaterims"
151 super(ClearwaterImsVnf, self).__init__(**kwargs)
152 self.logger = logging.getLogger("functest.ci.run_tests.orchestra")
153 self.logger.info("kwargs %s", (kwargs))
155 self.case_dir = pkg_resources.resource_filename(
156 'functest', 'opnfv_tests/vnf/ims/')
157 self.data_dir = CONST.__getattribute__('dir_ims_data')
158 self.test_dir = CONST.__getattribute__('dir_repo_vims_test')
159 self.created_resources = []
160 self.logger.info("%s VNF onboarding test starting", self.case_name)
163 self.config = CONST.__getattribute__(
164 'vnf_{}_config'.format(self.case_name))
165 except BaseException:
166 raise Exception("Orchestra VNF config file not found")
167 config_file = self.case_dir + self.config
170 get_config("mano", config_file),
173 self.logger.debug("Orchestrator configuration %s", self.mano)
175 self.details['orchestrator'] = dict(
176 name=self.mano['name'],
177 version=self.mano['version'],
183 get_config(self.case_name, config_file),
185 self.logger.debug("VNF configuration: %s", self.vnf)
187 self.details['vnf'] = dict(
188 name=self.vnf['name'],
191 self.details['test_vnf'] = dict(
195 # Orchestra base Data directory creation
196 if not os.path.exists(self.data_dir):
197 os.makedirs(self.data_dir)
199 self.images = get_config("tenant_images.orchestrator", config_file)
205 self.snaps_creds = None
208 """Prepare testscase (Additional pre-configuration steps)."""
209 super(ClearwaterImsVnf, self).prepare()
211 self.logger.info("Additional pre-configuration steps")
214 "tenant": self.tenant_name,
215 "username": self.tenant_name,
216 "password": self.tenant_name,
217 "auth_url": os_utils.get_credentials()['auth_url']
219 self.prepare_images()
220 self.prepare_flavor()
221 self.prepare_security_groups()
222 self.prepare_network()
223 self.prepare_floating_ip()
225 def prepare_images(self):
226 """Upload images if they doen't exist yet"""
227 self.logger.info("Upload images if they doen't exist yet")
228 for image_name, image_file in self.images.iteritems():
229 self.logger.info("image: %s, file: %s", image_name, image_file)
230 if image_file and image_name:
231 image = OpenStackImage(
233 ImageSettings(name=image_name,
236 image_file=image_file,
239 # self.created_resources.append(image);
241 def prepare_security_groups(self):
242 """Create Open Baton security group if it doesn't exist yet"""
244 "Creating security group for Open Baton if not yet existing...")
247 SecurityGroupRuleSettings(
248 sec_grp_name="orchestra-sec-group-allowall",
249 direction=Direction.ingress,
250 protocol=Protocol.tcp,
252 port_range_max=65535))
254 SecurityGroupRuleSettings(
255 sec_grp_name="orchestra-sec-group-allowall",
256 direction=Direction.egress,
257 protocol=Protocol.tcp,
259 port_range_max=65535))
261 SecurityGroupRuleSettings(
262 sec_grp_name="orchestra-sec-group-allowall",
263 direction=Direction.ingress,
264 protocol=Protocol.udp,
266 port_range_max=65535))
268 SecurityGroupRuleSettings(
269 sec_grp_name="orchestra-sec-group-allowall",
270 direction=Direction.egress,
271 protocol=Protocol.udp,
273 port_range_max=65535))
275 SecurityGroupRuleSettings(
276 sec_grp_name="orchestra-sec-group-allowall",
277 direction=Direction.ingress,
278 protocol=Protocol.icmp))
280 SecurityGroupRuleSettings(
281 sec_grp_name="orchestra-sec-group-allowall",
282 direction=Direction.egress,
283 protocol=Protocol.icmp))
285 # SecurityGroupRuleSettings(
286 # sec_grp_name="orchestra-sec-group-allowall",
287 # direction=Direction.ingress,
288 # protocol=Protocol.icmp,
290 # port_range_max=-1))
292 # SecurityGroupRuleSettings(
293 # sec_grp_name="orchestra-sec-group-allowall",
294 # direction=Direction.egress,
295 # protocol=Protocol.icmp,
297 # port_range_max=-1))
299 security_group = OpenStackSecurityGroup(
301 SecurityGroupSettings(
302 name="orchestra-sec-group-allowall",
303 rule_settings=sg_rules))
305 security_group_info = security_group.create()
306 self.created_resources.append(security_group)
307 self.mano['details']['sec_group'] = security_group_info.name
309 "Security group orchestra-sec-group-allowall prepared")
311 def prepare_flavor(self):
312 """Create Open Baton flavor if it doesn't exist yet"""
314 "Create Flavor for Open Baton NFVO if not yet existing")
316 flavor_settings = FlavorSettings(
317 name=self.mano['requirements']['flavor']['name'],
318 ram=self.mano['requirements']['flavor']['ram_min'],
319 disk=self.mano['requirements']['flavor']['disk'],
320 vcpus=self.mano['requirements']['flavor']['vcpus'])
321 flavor = OpenStackFlavor(self.snaps_creds, flavor_settings)
322 flavor_info = flavor.create()
323 self.created_resources.append(flavor)
324 self.mano['details']['flavor'] = {}
325 self.mano['details']['flavor']['name'] = flavor_settings.name
326 self.mano['details']['flavor']['id'] = flavor_info.id
328 def prepare_network(self):
329 """Create network/subnet/router if they doen't exist yet"""
331 "Creating network/subnet/router if they doen't exist yet...")
332 subnet_settings = SubnetSettings(
335 cidr="192.168.100.0/24")
336 network_settings = NetworkSettings(
339 subnet_settings=[subnet_settings])
340 orchestra_network = OpenStackNetwork(
341 self.snaps_creds, network_settings)
342 orchestra_network_info = orchestra_network.create()
343 self.mano['details']['network'] = {}
344 self.mano['details']['network']['id'] = orchestra_network_info.id
345 self.mano['details']['network']['name'] = orchestra_network_info.name
346 self.mano['details']['external_net_name'] = snaps_utils.\
347 get_ext_net_name(self.snaps_creds)
348 self.created_resources.append(orchestra_network)
349 orchestra_router = OpenStackRouter(
354 external_gateway=self.mano['details']['external_net_name'],
356 subnet_settings.name]))
357 orchestra_router.create()
358 self.created_resources.append(orchestra_router)
359 self.logger.info("Created network and router for Open Baton NFVO...")
361 def prepare_floating_ip(self):
362 """Select/Create Floating IP if it doesn't exist yet"""
363 self.logger.info("Retrieving floating IP for Open Baton NFVO")
364 neutron_client = snaps_utils.neutron_utils.neutron_client(
366 # Finding Tenant ID to check to which tenant the Floating IP belongs
367 tenant_id = os_utils.get_tenant_id(
368 os_utils.get_keystone_client(self.creds),
370 # Use os_utils to retrieve complete information of Floating IPs
371 floating_ips = os_utils.get_floating_ips(neutron_client)
373 # Filter Floating IPs with tenant id
374 for floating_ip in floating_ips:
375 # self.logger.info("Floating IP: %s", floating_ip)
376 if floating_ip.get('tenant_id') == tenant_id:
377 my_floating_ips.append(floating_ip.get('floating_ip_address'))
378 # Select if Floating IP exist else create new one
379 if len(my_floating_ips) >= 1:
380 # Get Floating IP object from snaps for clean up
381 snaps_floating_ips = snaps_utils.neutron_utils.get_floating_ips(
383 for my_floating_ip in my_floating_ips:
384 for snaps_floating_ip in snaps_floating_ips:
385 if snaps_floating_ip.ip == my_floating_ip:
386 self.mano['details']['fip'] = snaps_floating_ip
388 "Selected floating IP for Open Baton NFVO %s",
389 (self.mano['details']['fip'].ip))
391 if self.mano['details']['fip'] is not None:
394 self.logger.info("Creating floating IP for Open Baton NFVO")
395 self.mano['details']['fip'] = snaps_utils.neutron_utils.\
398 self.mano['details']['external_net_name'])
400 "Created floating IP for Open Baton NFVO %s",
401 (self.mano['details']['fip'].ip))
403 def get_vim_descriptor(self):
404 """"Create VIM descriptor to be used for onboarding"""
406 "Building VIM descriptor with PoP creds: %s",
408 # Depending on API version either tenant ID or project name must be
410 if os_utils.is_keystone_v3():
412 "Using v3 API of OpenStack... -> Using OS_PROJECT_ID")
413 project_id = os_utils.get_tenant_id(
414 os_utils.get_keystone_client(),
415 self.creds.get("project_name"))
418 "Using v2 API of OpenStack... -> Using OS_TENANT_NAME")
419 project_id = self.creds.get("tenant_name")
420 self.logger.debug("VIM project/tenant id: %s", project_id)
422 "name": "vim-instance",
423 "authUrl": self.creds.get("auth_url"),
424 "tenant": project_id,
425 "username": self.creds.get("username"),
426 "password": self.creds.get("password"),
428 self.mano['details']['sec_group']
433 "latitude": "52.525876",
434 "longitude": "13.314400"
437 self.logger.info("Built VIM descriptor: %s", vim_json)
440 def deploy_orchestrator(self):
441 self.logger.info("Deploying Open Baton...")
442 self.logger.info("Details: %s", self.mano['details'])
443 start_time = time.time()
445 self.logger.info("Creating orchestra instance...")
446 userdata = get_userdata(self.mano)
447 self.logger.info("flavor: %s\n"
450 self.mano['details']['flavor']['name'],
451 self.mano['requirements']['image'],
452 self.mano['details']['network']['id'])
453 self.logger.debug("userdata: %s\n", userdata)
455 image_settings = ImageSettings(
456 name=self.mano['requirements']['image'],
460 port_settings = PortSettings(
461 name='%s_port' % self.case_name,
462 network_name=self.mano['details']['network']['name'])
463 # build configuration of vm
464 orchestra_settings = VmInstanceSettings(
466 flavor=self.mano['details']['flavor']['name'],
467 port_settings=[port_settings],
468 security_group_names=[self.mano['details']['sec_group']],
470 orchestra_vm = OpenStackVmInstance(self.snaps_creds,
474 orchestra_vm.create()
475 self.created_resources.append(orchestra_vm)
476 self.mano['details']['id'] = orchestra_vm.get_vm_info()['id']
478 "Created orchestra instance: %s",
479 self.mano['details']['id'])
481 self.logger.info("Associating floating ip: '%s' to VM '%s' ",
482 self.mano['details']['fip'].ip,
484 nova_client = os_utils.get_nova_client()
485 if not os_utils.add_floating_ip(
487 self.mano['details']['id'],
488 self.mano['details']['fip'].ip):
489 duration = time.time() - start_time
490 self.details["orchestrator"].update(
491 status='FAIL', duration=duration)
492 self.logger.error("Cannot associate floating IP to VM.")
495 self.logger.info("Waiting for Open Baton NFVO to be up and running...")
499 self.mano['details']['fip'].ip,
504 "Open Baton NFVO is not started yet (%ss)",
510 duration = time.time() - start_time
511 self.details["orchestrator"].update(
512 status='FAIL', duration=duration)
513 self.logger.error("Open Baton is not started correctly")
516 self.logger.info("Waiting for all components to be up and running...")
518 duration = time.time() - start_time
519 self.details["orchestrator"].update(status='PASS', duration=duration)
520 self.logger.info("Deploy Open Baton NFVO: OK")
523 def deploy_vnf(self):
524 start_time = time.time()
525 self.logger.info("Deploying %s...", self.vnf['name'])
527 main_agent = MainAgent(
528 nfvo_ip=self.mano['details']['fip'].ip,
532 username=self.mano['credentials']['username'],
533 password=self.mano['credentials']['password'])
536 "Create %s Flavor if not existing", self.vnf['name'])
537 flavor_settings = FlavorSettings(
538 name=self.vnf['requirements']['flavor']['name'],
539 ram=self.vnf['requirements']['flavor']['ram_min'],
540 disk=self.vnf['requirements']['flavor']['disk'],
541 vcpus=self.vnf['requirements']['flavor']['vcpus'])
542 flavor = OpenStackFlavor(self.snaps_creds, flavor_settings)
543 flavor_info = flavor.create()
544 self.logger.debug("Flavor id: %s", flavor_info.id)
546 self.logger.info("Getting project 'default'...")
547 project_agent = main_agent.get_agent("project", "")
548 for project in json.loads(project_agent.find()):
549 if project.get("name") == "default":
550 self.mano['details']['project_id'] = project.get("id")
551 self.logger.info("Found project 'default': %s", project)
554 vim_json = self.get_vim_descriptor()
555 self.logger.info("Registering VIM: %s", vim_json)
557 main_agent.get_agent(
558 "vim", project_id=self.mano['details']['project_id']).create(
559 entity=json.dumps(vim_json))
561 market_agent = main_agent.get_agent(
562 "market", project_id=self.mano['details']['project_id'])
565 self.logger.info("sending: %s", self.vnf['descriptor']['url'])
566 nsd = market_agent.create(entity=self.vnf['descriptor']['url'])
567 if nsd.get('id') is None:
568 self.logger.error("NSD not onboarded correctly")
569 duration = time.time() - start_time
570 self.details["vnf"].update(status='FAIL', duration=duration)
572 self.mano['details']['nsd_id'] = nsd.get('id')
573 self.logger.info("Onboarded NSD: " + nsd.get("name"))
575 nsr_agent = main_agent.get_agent(
576 "nsr", project_id=self.mano['details']['project_id'])
578 self.mano['details']['nsr'] = nsr_agent.create(
579 self.mano['details']['nsd_id'])
580 except NfvoException as exc:
581 self.logger.error(exc.message)
582 duration = time.time() - start_time
583 self.details["vnf"].update(status='FAIL', duration=duration)
586 if self.mano['details']['nsr'].get('code') is not None:
588 "%s cannot be deployed: %s -> %s",
590 self.mano['details']['nsr'].get('code'),
591 self.mano['details']['nsr'].get('message'))
592 self.logger.error("%s cannot be deployed", self.vnf['name'])
593 duration = time.time() - start_time
594 self.details["vnf"].update(status='FAIL', duration=duration)
598 self.logger.info("Waiting for NSR to go to ACTIVE...")
599 while self.mano['details']['nsr'].get("status") != 'ACTIVE' \
600 and self.mano['details']['nsr'].get("status") != 'ERROR':
602 self.logger.info("NSR is not yet ACTIVE... (%ss)", 60 * timeout)
604 self.logger.error("INACTIVE NSR after %s sec..", 60 * timeout)
605 duration = time.time() - start_time
606 self.details["vnf"].update(status='FAIL', duration=duration)
609 self.mano['details']['nsr'] = json.loads(
610 nsr_agent.find(self.mano['details']['nsr'].get('id')))
612 duration = time.time() - start_time
613 if self.mano['details']['nsr'].get("status") == 'ACTIVE':
614 self.details["vnf"].update(status='PASS', duration=duration)
615 self.logger.info("Sleep for 60s to ensure that all "
616 "services are up and running...")
620 self.details["vnf"].update(status='FAIL', duration=duration)
621 self.logger.error("NSR: %s", self.mano['details'].get('nsr'))
627 "Testing VNF Clearwater IMS is not yet implemented...")
628 start_time = time.time()
630 duration = time.time() - start_time
631 self.details["test_vnf"].update(status='PASS', duration=duration)
632 self.logger.info("Test VNF: OK")
636 self.logger.info("Cleaning %s...", self.case_name)
638 main_agent = MainAgent(
639 nfvo_ip=self.mano['details']['fip'].ip,
643 username=self.mano['credentials']['username'],
644 password=self.mano['credentials']['password'])
645 self.logger.info("Terminating %s...", self.vnf['name'])
646 if (self.mano['details'].get('nsr')):
647 main_agent.get_agent(
649 project_id=self.mano['details']['project_id']).delete(
650 self.mano['details']['nsr'].get('id'))
651 self.logger.info("Sleeping 60 seconds...")
654 self.logger.info("No need to terminate the VNF...")
655 # os_utils.delete_instance(nova_client=os_utils.get_nova_client(),
656 # instance_id=self.mano_instance_id)
657 except (NfvoException, KeyError) as exc:
658 self.logger.error('Unexpected error cleaning - %s', exc)
661 neutron_client = os_utils.get_neutron_client(self.creds)
662 self.logger.info("Deleting Open Baton Port...")
663 port = snaps_utils.neutron_utils.get_port(
665 port_name='%s_port' % self.case_name)
666 snaps_utils.neutron_utils.delete_port(neutron_client, port)
668 except Exception as exc:
669 self.logger.error('Unexpected error cleaning - %s', exc)
671 self.logger.info("Deleting Open Baton Floating IP...")
672 snaps_utils.neutron_utils.delete_floating_ip(
673 neutron_client, self.mano['details']['fip'])
674 except Exception as exc:
675 self.logger.error('Unexpected error cleaning - %s', exc)
677 for resource in reversed(self.created_resources):
679 self.logger.info("Cleaning %s", str(resource))
681 except Exception as exc:
682 self.logger.error('Unexpected error cleaning - %s', exc)
683 super(ClearwaterImsVnf, self).clean()