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
16 import functest.core.vnf_base as vnf_base
17 import functest.utils.functest_logger as ft_logger
18 import functest.utils.functest_utils as ft_utils
19 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
26 # ----------------------------------------------------------
30 # -----------------------------------------------------------
33 def get_config(parameter, file):
35 Returns the value of a given parameter in file.yaml
36 parameter must be given in string format with dots
37 Example: general.openstack.image_name
40 file_yaml = yaml.safe_load(f)
43 for element in parameter.split("."):
44 value = value.get(element)
46 raise ValueError("The parameter %s is not defined in"
47 " %s" % (parameter, file))
51 def download_and_add_image_on_glance(glance, image_name,
54 if not os.path.exists(dest_path):
55 os.makedirs(dest_path)
56 file_name = image_url.rsplit('/')[-1]
57 if not ft_utils.download_url(image_url, dest_path):
59 image = os_utils.create_glance_image(
60 glance, image_name, dest_path + file_name)
66 def servertest(host, port):
67 args = socket.getaddrinfo(host, port, socket.AF_INET, socket.SOCK_STREAM)
68 for family, socktype, proto, canonname, sockaddr in args:
69 s = socket.socket(family, socktype, proto)
79 class ImsVnf(vnf_base.VnfOnBoardingBase):
81 def __init__(self, project='functest', case='orchestra_ims',
83 super(ImsVnf, self).__init__(project, case, repo, cmd)
84 self.ob_password = "openbaton"
85 self.ob_username = "admin"
88 self.ob_ip = "localhost"
89 self.ob_instance_id = ""
90 self.logger = ft_logger.Logger("orchestra_ims").getLogger()
91 self.case_dir = os.path.join(CONST.dir_functest_test, 'vnf/ims/')
92 self.data_dir = CONST.dir_ims_data
93 self.test_dir = CONST.dir_repo_vims_test
94 self.ob_projectid = ""
95 self.keystone_client = os_utils.get_keystone_client()
98 self.main_agent = None
99 # vIMS Data directory creation
100 if not os.path.exists(self.data_dir):
101 os.makedirs(self.data_dir)
102 # Retrieve the configuration
104 self.config = CONST.__getattribute__(
105 'vnf_{}_config'.format(self.case_name))
107 raise Exception("Orchestra VNF config file not found")
108 config_file = self.case_dir + self.config
109 self.imagename = get_config("openbaton.imagename", config_file)
110 self.bootstrap_link = get_config("openbaton.bootstrap_link",
112 self.bootstrap_config_link = get_config(
113 "openbaton.bootstrap_config_link", config_file)
114 self.market_link = get_config("openbaton.marketplace_link",
116 self.images = get_config("tenant_images", config_file)
117 self.ims_conf = get_config("vIMS", config_file)
119 def deploy_orchestrator(self, **kwargs):
120 self.logger.info("Additional pre-configuration steps")
121 nova_client = os_utils.get_nova_client()
122 neutron_client = os_utils.get_neutron_client()
123 glance_client = os_utils.get_glance_client()
125 # Import images if needed
127 self.logger.info("Upload some OS images if it doesn't exist")
128 temp_dir = os.path.join(self.data_dir, "tmp/")
129 for image_name, image_url in self.images.iteritems():
130 self.logger.info("image: %s, url: %s" % (image_name, image_url))
132 image_id = os_utils.get_image_id(glance_client,
134 self.logger.info("image_id: %s" % image_id)
136 self.logger.error("Unexpected error: %s" % sys.exc_info()[0])
139 self.logger.info("""%s image doesn't exist on glance repository. Try
140 downloading this image and upload on glance !""" % image_name)
141 image_id = download_and_add_image_on_glance(glance_client,
147 "Failed to find or upload required OS "
148 "image for this deployment")
149 network_dic = os_utils.create_network_full(neutron_client,
151 "openbaton_mgmt_subnet",
155 # orchestrator VM flavor
156 self.logger.info("Check if Flavor is available, if not create one")
157 flavor_exist, flavor_id = os_utils.get_or_create_flavor(
163 self.logger.debug("Flavor id: %s" % flavor_id)
166 self.logger.error("There has been a problem when creating the "
169 network_id = network_dic["net_id"]
171 self.logger.info("Creating floating IP for VM in advance...")
172 floatip_dic = os_utils.create_floating_ip(neutron_client)
173 floatip = floatip_dic['fip_addr']
176 self.logger.error("Cannot create floating IP.")
178 userdata = "#!/bin/bash\n"
179 userdata += "echo \"Executing userdata...\"\n"
180 userdata += "set -x\n"
181 userdata += "set -e\n"
182 userdata += "echo \"Set nameserver to '8.8.8.8'...\"\n"
183 userdata += "echo \"nameserver 8.8.8.8\" >> /etc/resolv.conf\n"
184 userdata += "echo \"Install curl...\"\n"
185 userdata += "apt-get install curl\n"
186 userdata += "echo \"Inject public key...\"\n"
187 userdata += ("echo \"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCuPXrV3"
188 "geeHc6QUdyUr/1Z+yQiqLcOskiEGBiXr4z76MK4abiFmDZ18OMQlc"
189 "fl0p3kS0WynVgyaOHwZkgy/DIoIplONVr2CKBKHtPK+Qcme2PVnCtv"
190 "EqItl/FcD+1h5XSQGoa+A1TSGgCod/DPo+pes0piLVXP8Ph6QS1k7S"
191 "ic7JDeRQ4oT1bXYpJ2eWBDMfxIWKZqcZRiGPgMIbJ1iEkxbpeaAd9O"
192 "4MiM9nGCPESmed+p54uYFjwEDlAJZShcAZziiZYAvMZhvAhe6USljc"
193 "7YAdalAnyD/jwCHuwIrUw/lxo7UdNCmaUxeobEYyyFA1YVXzpNFZya"
194 "XPGAAYIJwEq/ openbaton@opnfv\" >> /home/ubuntu/.ssh/aut"
196 userdata += "echo \"Download bootstrap...\"\n"
197 userdata += ("curl -s %s "
198 "> ./bootstrap\n" % self.bootstrap_link)
199 userdata += ("curl -s %s"
200 "> ./config_file\n" % self.bootstrap_config_link)
201 userdata += ("echo \"Disable usage of mysql...\"\n")
202 userdata += "sed -i s/mysql=.*/mysql=no/g /config_file\n"
203 userdata += ("echo \"Setting 'rabbitmq_broker_ip' to '%s'\"\n"
205 userdata += ("sed -i s/rabbitmq_broker_ip=localhost/rabbitmq_broker_ip"
206 "=%s/g /config_file\n" % floatip)
207 userdata += "echo \"Set autostart of components to 'false'\"\n"
208 userdata += "export OPENBATON_COMPONENT_AUTOSTART=false\n"
209 userdata += "echo \"Execute bootstrap...\"\n"
210 bootstrap = "sh ./bootstrap release -configFile=./config_file"
211 userdata += bootstrap + "\n"
212 userdata += "echo \"Setting 'nfvo.plugin.timeout' to '300000'\"\n"
213 userdata += ("echo \"nfvo.plugin.timeout=300000\" >> "
214 "/etc/openbaton/openbaton-nfvo.properties\n")
215 userdata += "echo \"Starting NFVO\"\n"
216 userdata += "service openbaton-nfvo restart\n"
217 userdata += "echo \"Starting Generic VNFM\"\n"
218 userdata += "service openbaton-vnfm-generic restart\n"
219 userdata += "echo \"...end of userdata...\"\n"
221 sg_id = os_utils.create_security_group_full(neutron_client,
222 "orchestra-sec-group",
225 os_utils.create_secgroup_rule(neutron_client, sg_id, "ingress",
227 os_utils.create_secgroup_rule(neutron_client, sg_id, "egress",
229 os_utils.create_secgroup_rule(neutron_client, sg_id, "ingress",
231 os_utils.create_secgroup_rule(neutron_client, sg_id, "ingress",
233 os_utils.create_secgroup_rule(neutron_client, sg_id, "egress",
235 os_utils.create_secgroup_rule(neutron_client, sg_id, "egress",
238 self.logger.info("Security group set")
240 self.logger.info("Create instance....")
241 self.logger.info("flavor: m1.medium\n"
245 % (self.imagename, network_id, userdata))
247 instance = os_utils.create_instance_and_wait_for_active(
249 os_utils.get_image_id(glance_client, self.imagename),
251 "orchestra-openbaton",
255 self.ob_instance_id = instance.id
257 self.logger.info("Adding sec group to orchestra instance")
258 os_utils.add_secgroup_to_instance(nova_client,
259 self.ob_instance_id, sg_id)
261 self.logger.info("Associating floating ip: '%s' to VM '%s' "
262 % (floatip, "orchestra-openbaton"))
263 if not os_utils.add_floating_ip(nova_client, instance.id, floatip):
264 self.step_failure("Cannot associate floating IP to VM.")
266 self.logger.info("Waiting for Open Baton NFVO to be up and running...")
269 if servertest(floatip, "8080"):
273 "Open Baton NFVO is not started yet (%ss)" %
279 self.step_failure("Open Baton is not started correctly")
282 self.ob_password = "openbaton"
283 self.ob_username = "admin"
284 self.ob_https = False
285 self.ob_port = "8080"
287 self.logger.info("Deploy Open Baton NFVO: OK")
289 def deploy_vnf(self):
290 self.logger.info("Starting vIMS Deployment...")
292 self.main_agent = MainAgent(nfvo_ip=self.ob_ip,
293 nfvo_port=self.ob_port,
296 username=self.ob_username,
297 password=self.ob_password)
299 self.logger.info("Getting project 'default'...")
300 project_agent = self.main_agent.get_agent("project", self.ob_projectid)
301 for p in json.loads(project_agent.find()):
302 if p.get("name") == "default":
303 self.ob_projectid = p.get("id")
304 self.logger.info("Found project 'default': %s" % p)
307 self.logger.debug("project id: %s" % self.ob_projectid)
308 if self.ob_projectid == "":
309 self.step_failure("Default project id was not found!")
312 "name": "vim-instance",
313 "authUrl": os_utils.get_credentials().get("auth_url"),
314 "tenant": os_utils.get_credentials().get("tenant_name"),
315 "username": os_utils.get_credentials().get("username"),
316 "password": os_utils.get_credentials().get("password"),
319 "orchestra-sec-group"
324 "latitude": "52.525876",
325 "longitude": "13.314400"
329 self.logger.debug("Registering VIM: %s" % vim_json)
331 self.main_agent.get_agent(
333 project_id=self.ob_projectid).create(entity=json.dumps(vim_json))
335 market_agent = self.main_agent.get_agent("market",
336 project_id=self.ob_projectid)
340 self.logger.info("sending: %s" % self.market_link)
341 nsd = market_agent.create(entity=self.market_link)
342 self.logger.info("Onboarded NSD: " + nsd.get("name"))
343 except NfvoException as e:
344 self.step_failure(e.message)
346 nsr_agent = self.main_agent.get_agent("nsr",
347 project_id=self.ob_projectid)
348 nsd_id = nsd.get('id')
350 self.step_failure("NSD not onboarded correctly")
353 self.nsr = nsr_agent.create(nsd_id)
354 except NfvoException as e:
355 self.step_failure(e.message)
357 if self.nsr.get('code') is not None:
359 "vIMS cannot be deployed: %s -> %s" %
360 (self.nsr.get('code'), self.nsr.get('message')))
361 self.step_failure("vIMS cannot be deployed")
364 self.logger.info("Waiting for NSR to go to ACTIVE...")
365 while self.nsr.get("status") != 'ACTIVE' and self.nsr.get(
366 "status") != 'ERROR':
369 self.step_failure("After %s sec the NSR did not go to ACTIVE.."
372 self.nsr = json.loads(nsr_agent.find(self.nsr.get('id')))
374 if self.nsr.get("status") == 'ACTIVE':
375 deploy_vnf = {'status': "PASS", 'result': self.nsr}
376 self.logger.info("Deploy VNF: OK")
378 deploy_vnf = {'status': "FAIL", 'result': self.nsr}
379 self.step_failure("Deploy VNF: ERROR")
380 self.ob_nsr_id = self.nsr.get("id")
382 "Sleep for 60s to ensure that all services are up and running...")
387 # Adaptations probably needed
388 # code used for cloudify_ims
389 # ruby client on jumphost calling the vIMS on the SUT
391 "Testing if %s works properly..." %
392 self.nsr.get('name'))
393 for vnfr in self.nsr.get('vnfr'):
395 "Checking ports %s of VNF %s" %
397 vnfr.get('name')).get('ports'),
399 for vdu in vnfr.get('vdu'):
400 for vnfci in vdu.get('vnfc_instance'):
402 "Checking ports of VNFC instance %s" %
403 vnfci.get('hostname'))
404 for floatingIp in vnfci.get('floatingIps'):
407 (vnfci.get('hostname'), floatingIp.get('ip')))
408 for port in self.ims_conf.get(
409 vnfr.get('name')).get('ports'):
410 if servertest(floatingIp.get('ip'), port):
412 "VNFC instance %s is reachable at %s:%s" %
413 (vnfci.get('hostname'),
414 floatingIp.get('ip'),
418 "VNFC instance %s is not reachable "
419 "at %s:%s" % (vnfci.get('hostname'),
420 floatingIp.get('ip'), port))
421 self.step_failure("Test VNF: ERROR")
422 self.logger.info("Test VNF: OK")
426 self.main_agent.get_agent(
428 project_id=self.ob_projectid).delete(self.ob_nsr_id)
430 os_utils.delete_instance(nova_client=os_utils.get_nova_client(),
431 instance_id=self.ob_instance_id)
432 # TODO question is the clean removing also the VM?
433 # I think so since is goinf to remove the tenant...
434 super(ImsVnf, self).clean()
436 def main(self, **kwargs):
437 self.logger.info("Orchestra IMS VNF onboarding test starting")
439 self.logger.info("Orchestra IMS VNF onboarding test executed")
440 if self.criteria is "PASS":
443 return self.EX_RUN_ERROR
447 return self.main(**kwargs)
450 if __name__ == '__main__':
452 test.deploy_orchestrator()