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
18 from functest.opnfv_tests.vnf.ims.clearwater import Clearwater
19 import functest.opnfv_tests.vnf.ims.clearwater_ims_base as clearwater_ims_base
20 from functest.opnfv_tests.vnf.ims.orchestrator_cloudify import Orchestrator
21 from functest.utils.constants import CONST
22 import functest.utils.functest_utils as ft_utils
23 import functest.utils.openstack_utils as os_utils
26 class CloudifyIms(clearwater_ims_base.ClearwaterOnBoardingBase):
28 def __init__(self, **kwargs):
29 if "case_name" not in kwargs:
30 kwargs["case_name"] = "cloudify_ims"
31 super(CloudifyIms, self).__init__(**kwargs)
32 self.logger = logging.getLogger(__name__)
34 # Retrieve the configuration
36 self.config = CONST.__getattribute__(
37 'vnf_{}_config'.format(self.case_name))
39 raise Exception("VNF config file not found")
41 config_file = os.path.join(self.case_dir, self.config)
42 self.orchestrator = dict(
43 requirements=get_config("cloudify.requirements", config_file),
44 blueprint=get_config("cloudify.blueprint", config_file),
45 inputs=get_config("cloudify.inputs", config_file)
47 self.logger.debug("Orchestrator configuration: %s" % self.orchestrator)
49 blueprint=get_config("clearwater.blueprint", config_file),
50 deployment_name=get_config("clearwater.deployment_name",
52 inputs=get_config("clearwater.inputs", config_file),
53 requirements=get_config("clearwater.requirements", config_file)
55 self.logger.debug("VNF configuration: %s" % self.vnf)
57 self.images = get_config("tenant_images", config_file)
58 self.logger.info("Images needed for vIMS: %s" % self.images)
60 def deploy_orchestrator(self, **kwargs):
62 self.logger.info("Additional pre-configuration steps")
63 self.neutron_client = os_utils.get_neutron_client(self.admin_creds)
64 self.glance_client = os_utils.get_glance_client(self.admin_creds)
65 self.keystone_client = os_utils.get_keystone_client(self.admin_creds)
66 self.nova_client = os_utils.get_nova_client(self.admin_creds)
69 self.logger.info("Upload some OS images if it doesn't exist")
70 temp_dir = os.path.join(self.data_dir, "tmp/")
71 for image_name, image_url in self.images.iteritems():
72 self.logger.info("image: %s, url: %s" % (image_name, image_url))
74 image_id = os_utils.get_image_id(self.glance_client,
76 self.logger.debug("image_id: %s" % image_id)
78 self.logger.error("Unexpected error: %s" % sys.exc_info()[0])
81 self.logger.info("""%s image doesn't exist on glance repository. Try
82 downloading this image and upload on glance !""" % image_name)
83 image_id = download_and_add_image_on_glance(self.glance_client,
89 "Failed to find or upload required OS "
90 "image for this deployment")
91 # Need to extend quota
92 self.logger.info("Update security group quota for this tenant")
93 tenant_id = os_utils.get_tenant_id(self.keystone_client,
95 self.logger.debug("Tenant id found %s" % tenant_id)
96 if not os_utils.update_sg_quota(self.neutron_client,
98 self.step_failure("Failed to update security group quota" +
99 " for tenant " + self.tenant_name)
100 self.logger.debug("group quota extended")
102 # start the deployment of cloudify
103 public_auth_url = os_utils.get_endpoint('identity')
105 self.logger.debug("CFY inputs: %s" % self.orchestrator['inputs'])
106 cfy = Orchestrator(self.data_dir, self.orchestrator['inputs'])
107 self.orchestrator['object'] = cfy
108 self.logger.debug("Orchestrator object created")
110 self.logger.debug("Tenant name: %s" % self.tenant_name)
112 cfy.set_credentials(username=self.tenant_name,
113 password=self.tenant_name,
114 tenant_name=self.tenant_name,
115 auth_url=public_auth_url)
116 self.logger.info("Credentials set in CFY")
118 # orchestrator VM flavor
119 self.logger.info("Check Flavor is available, if not create one")
120 self.logger.debug("Flavor details %s " %
121 self.orchestrator['requirements']['ram_min'])
122 flavor_exist, flavor_id = os_utils.get_or_create_flavor(
124 self.orchestrator['requirements']['ram_min'],
128 self.logger.debug("Flavor id: %s" % flavor_id)
131 self.logger.info("Available flavors are: ")
132 self.logger.info(self.nova_client.flavor.list())
133 self.step_failure("Failed to find required flavor"
134 "for this deployment")
135 cfy.set_flavor_id(flavor_id)
136 self.logger.debug("Flavor OK")
138 # orchestrator VM image
139 self.logger.debug("Orchestrator image")
140 if 'os_image' in self.orchestrator['requirements'].keys():
141 image_id = os_utils.get_image_id(
143 self.orchestrator['requirements']['os_image'])
144 self.logger.debug("Orchestrator image id: %s" % image_id)
146 self.logger.error("CFY image not found")
147 self.step_failure("Failed to find required OS image"
148 " for cloudify manager")
150 self.step_failure("Failed to find required OS image"
151 " for cloudify manager")
153 cfy.set_image_id(image_id)
154 self.logger.debug("Orchestrator image set")
156 self.logger.debug("Get External network")
157 ext_net = os_utils.get_external_net(self.neutron_client)
158 self.logger.debug("External network: %s" % ext_net)
160 self.step_failure("Failed to get external network")
162 cfy.set_external_network_name(ext_net)
163 self.logger.debug("CFY External network set")
165 self.logger.debug("get resolvconf")
166 ns = ft_utils.get_resolvconf_ns()
168 cfy.set_nameservers(ns)
169 self.logger.debug("Resolvconf set")
171 self.logger.info("Prepare virtualenv for cloudify-cli")
172 venv_scrit_dir = os.path.join(self.case_dir, "create_venv.sh")
173 cmd = "chmod +x " + venv_scrit_dir
174 ft_utils.execute_command(cmd)
176 cmd = venv_scrit_dir + " " + self.data_dir
177 ft_utils.execute_command(cmd)
179 cfy.download_manager_blueprint(
180 self.orchestrator['blueprint']['url'],
181 self.orchestrator['blueprint']['branch'])
183 error = cfy.deploy_manager()
185 self.logger.error(error)
186 return {'status': 'FAIL', 'result': error}
188 return {'status': 'PASS', 'result': ''}
190 def deploy_vnf(self):
191 cw = Clearwater(self.vnf['inputs'], self.orchestrator['object'],
193 self.vnf['object'] = cw
195 self.logger.info("Collect flavor id for all clearwater vm")
196 flavor_exist, flavor_id = os_utils.get_or_create_flavor(
198 self.vnf['requirements']['ram_min'],
202 self.logger.debug("Flavor id: %s" % flavor_id)
204 self.logger.info("Available flavors are: ")
205 self.logger.info(self.nova_client.flavor.list())
206 self.step_failure("Failed to find required flavor"
207 " for this deployment")
209 cw.set_flavor_id(flavor_id)
212 if 'os_image' in self.vnf['requirements'].keys():
213 image_id = os_utils.get_image_id(
214 self.glance_client, self.vnf['requirements']['os_image'])
216 self.step_failure("Failed to find required OS image"
217 " for clearwater VMs")
219 self.step_failure("Failed to find required OS image"
220 " for clearwater VMs")
222 cw.set_image_id(image_id)
224 ext_net = os_utils.get_external_net(self.neutron_client)
226 self.step_failure("Failed to get external network")
228 cw.set_external_network_name(ext_net)
230 error = cw.deploy_vnf(self.vnf['blueprint'])
232 self.logger.error(error)
233 return {'status': 'FAIL', 'result': error}
235 return {'status': 'PASS', 'result': ''}
238 script = "source {0}venv_cloudify/bin/activate; "
240 script += "cfy status | grep -Eo \"([0-9]{{1,3}}\.){{3}}[0-9]{{1,3}}\""
241 cmd = "/bin/bash -c '" + script.format(self.data_dir) + "'"
244 self.logger.debug("Trying to get clearwater manager IP ... ")
245 mgr_ip = os.popen(cmd).read()
246 mgr_ip = mgr_ip.splitlines()[0]
248 self.step_failure("Unable to retrieve the IP of the "
249 "cloudify manager server !")
251 self.logger.info('Cloudify Manager: %s', mgr_ip)
252 api_url = 'http://{0}/api/v2/deployments/{1}/outputs'.format(
253 mgr_ip, self.vnf['deployment_name'])
254 dep_outputs = requests.get(api_url)
255 self.logger.info(api_url)
256 outputs = dep_outputs.json()['outputs']
257 self.logger.info("Deployment outputs: %s", outputs)
258 dns_ip = outputs['dns_ip']
259 ellis_ip = outputs['ellis_ip']
260 self.config_ellis(ellis_ip)
263 vims_test_result = self.run_clearwater_live_test(
265 public_domain=self.inputs["public_domain"])
266 if vims_test_result != '':
267 return {'status': 'PASS', 'result': vims_test_result}
269 return {'status': 'FAIL', 'result': ''}
272 self.vnf['object'].undeploy_vnf()
273 self.orchestrator['object'].undeploy_manager()
274 super(CloudifyIms, self).clean()
276 def main(self, **kwargs):
277 self.logger.info("Cloudify IMS VNF onboarding test starting")
279 self.logger.info("Cloudify IMS VNF onboarding test executed")
280 if self.result is "PASS":
283 return self.EX_RUN_ERROR
287 return self.main(**kwargs)
290 # ----------------------------------------------------------
294 # -----------------------------------------------------------
295 def get_config(parameter, file):
297 Returns the value of a given parameter in file.yaml
298 parameter must be given in string format with dots
299 Example: general.openstack.image_name
301 with open(file) as f:
302 file_yaml = yaml.safe_load(f)
305 for element in parameter.split("."):
306 value = value.get(element)
308 raise ValueError("The parameter %s is not defined in"
309 " reporting.yaml" % parameter)
313 def download_and_add_image_on_glance(glance, image_name, image_url, data_dir):
315 if not os.path.exists(dest_path):
316 os.makedirs(dest_path)
317 file_name = image_url.rsplit('/')[-1]
318 if not ft_utils.download_url(image_url, dest_path):
321 image = os_utils.create_glance_image(
322 glance, image_name, dest_path + file_name)