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
17 from functest.opnfv_tests.vnf.ims.clearwater import Clearwater
18 import functest.opnfv_tests.vnf.ims.clearwater_ims_base as clearwater_ims_base
19 from functest.opnfv_tests.vnf.ims.orchestrator_cloudify import Orchestrator
20 from functest.utils.constants import CONST
21 import functest.utils.functest_utils as ft_utils
22 import functest.utils.openstack_utils as os_utils
24 __author__ = "Valentin Boucher <valentin.boucher@orange.com>"
27 class CloudifyIms(clearwater_ims_base.ClearwaterOnBoardingBase):
28 """Clearwater vIMS deployed with Cloudify Orchestrator Case"""
30 def __init__(self, **kwargs):
31 if "case_name" not in kwargs:
32 kwargs["case_name"] = "cloudify_ims"
33 super(CloudifyIms, self).__init__(**kwargs)
34 self.logger = logging.getLogger(__name__)
35 self.neutron_client = ''
36 self.glance_client = ''
37 self.keystone_client = ''
40 # Retrieve the configuration
42 self.config = CONST.__getattribute__(
43 'vnf_{}_config'.format(self.case_name))
45 raise Exception("VNF config file not found")
47 config_file = os.path.join(self.case_dir, self.config)
48 self.orchestrator = dict(
49 requirements=get_config("cloudify.requirements", config_file),
50 blueprint=get_config("cloudify.blueprint", config_file),
51 inputs=get_config("cloudify.inputs", config_file)
53 self.logger.debug("Orchestrator configuration: %s", self.orchestrator)
55 blueprint=get_config("clearwater.blueprint", config_file),
56 deployment_name=get_config("clearwater.deployment_name",
58 inputs=get_config("clearwater.inputs", config_file),
59 requirements=get_config("clearwater.requirements", config_file)
61 self.logger.debug("VNF configuration: %s", self.vnf)
63 self.images = get_config("tenant_images", config_file)
64 self.logger.info("Images needed for vIMS: %s", self.images)
66 def deploy_orchestrator(self):
68 self.logger.info("Additional pre-configuration steps")
69 self.neutron_client = os_utils.get_neutron_client(self.admin_creds)
70 self.glance_client = os_utils.get_glance_client(self.admin_creds)
71 self.keystone_client = os_utils.get_keystone_client(self.admin_creds)
72 self.nova_client = os_utils.get_nova_client(self.admin_creds)
75 self.logger.info("Upload some OS images if it doesn't exist")
76 temp_dir = os.path.join(self.data_dir, "tmp/")
77 for image_name, image_url in self.images.iteritems():
78 self.logger.info("image: %s, url: %s", image_name, image_url)
80 image_id = os_utils.get_image_id(self.glance_client,
82 self.logger.debug("image_id: %s", image_id)
84 self.logger.error("Unexpected error: %s", sys.exc_info()[0])
87 self.logger.info("""%s image does not exist on glance repo.
88 Try downloading this image
89 and upload on glance !""",
91 image_id = os_utils.download_and_add_image_on_glance(
96 # Need to extend quota
97 self.logger.info("Update security group quota for this tenant")
98 tenant_id = os_utils.get_tenant_id(self.keystone_client,
100 self.logger.debug("Tenant id found %s", tenant_id)
101 if not os_utils.update_sg_quota(self.neutron_client,
103 self.logger.error("Failed to update security group quota"
104 " for tenant " + self.tenant_name)
106 self.logger.debug("group quota extended")
108 # start the deployment of cloudify
109 public_auth_url = os_utils.get_endpoint('identity')
111 self.logger.debug("CFY inputs: %s", self.orchestrator['inputs'])
112 cfy = Orchestrator(self.data_dir, self.orchestrator['inputs'])
113 self.orchestrator['object'] = cfy
114 self.logger.debug("Orchestrator object created")
116 self.logger.debug("Tenant name: %s", self.tenant_name)
118 cfy.set_credentials(username=self.tenant_name,
119 password=self.tenant_name,
120 tenant_name=self.tenant_name,
121 auth_url=public_auth_url)
122 self.logger.info("Credentials set in CFY")
124 # orchestrator VM flavor
125 self.logger.info("Check Flavor is available, if not create one")
126 self.logger.debug("Flavor details %s ",
127 self.orchestrator['requirements']['ram_min'])
128 flavor_exist, flavor_id = os_utils.get_or_create_flavor(
130 self.orchestrator['requirements']['ram_min'],
134 self.logger.debug("Flavor id: %s", flavor_id)
137 self.logger.info("Available flavors are: ")
138 self.logger.info(self.nova_client.flavor.list())
140 cfy.set_flavor_id(flavor_id)
141 self.logger.debug("Flavor OK")
143 # orchestrator VM image
144 self.logger.debug("Orchestrator image")
145 if 'os_image' in self.orchestrator['requirements'].keys():
146 image_id = os_utils.get_image_id(
148 self.orchestrator['requirements']['os_image'])
149 self.logger.debug("Orchestrator image id: %s", image_id)
151 self.logger.error("CFY image not found")
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 cfy.set_external_network_name(ext_net)
161 self.logger.debug("CFY External network set")
163 self.logger.debug("get resolvconf")
164 ns = ft_utils.get_resolvconf_ns()
166 cfy.set_nameservers(ns)
167 self.logger.debug("Resolvconf set")
169 self.logger.info("Prepare virtualenv for cloudify-cli")
170 venv_scrit_dir = os.path.join(self.case_dir, "create_venv.sh")
171 cmd = "bash {} {}".format(venv_scrit_dir, self.data_dir)
172 ft_utils.execute_command(cmd)
174 cfy.download_manager_blueprint(
175 self.orchestrator['blueprint']['url'],
176 self.orchestrator['blueprint']['branch'])
178 error = cfy.deploy_manager()
180 self.logger.error(error)
181 return {'status': 'FAIL', 'result': error}
183 return {'status': 'PASS', 'result': ''}
185 def deploy_vnf(self):
186 cw = Clearwater(self.vnf['inputs'], self.orchestrator['object'],
188 self.vnf['object'] = cw
190 self.logger.info("Collect flavor id for all clearwater vm")
191 flavor_exist, flavor_id = os_utils.get_or_create_flavor(
193 self.vnf['requirements']['ram_min'],
197 self.logger.debug("Flavor id: %s", flavor_id)
199 self.logger.info("Available flavors are: ")
200 self.logger.info(self.nova_client.flavor.list())
202 cw.set_flavor_id(flavor_id)
205 if 'os_image' in self.vnf['requirements'].keys():
206 image_id = os_utils.get_image_id(
207 self.glance_client, self.vnf['requirements']['os_image'])
209 cw.set_image_id(image_id)
210 ext_net = os_utils.get_external_net(self.neutron_client)
211 cw.set_external_network_name(ext_net)
213 error = cw.deploy_vnf(self.vnf['blueprint'])
215 self.logger.error(error)
216 return {'status': 'FAIL', 'result': error}
218 return {'status': 'PASS', 'result': ''}
221 script = "source {0}venv_cloudify/bin/activate; "
223 script += "cfy status | grep -Eo \"([0-9]{{1,3}}\.){{3}}[0-9]{{1,3}}\""
224 cmd = "/bin/bash -c '" + script.format(self.data_dir) + "'"
227 self.logger.debug("Trying to get clearwater manager IP ... ")
228 mgr_ip = os.popen(cmd).read()
229 mgr_ip = mgr_ip.splitlines()[0]
231 self.logger.exception("Unable to retrieve the IP of the "
232 "cloudify manager server !")
234 self.logger.info('Cloudify Manager: %s', mgr_ip)
235 api_url = 'http://{0}/api/v2/deployments/{1}/outputs'.format(
236 mgr_ip, self.vnf['deployment_name'])
237 dep_outputs = requests.get(api_url)
238 self.logger.info(api_url)
239 outputs = dep_outputs.json()['outputs']
240 self.logger.info("Deployment outputs: %s", outputs)
241 dns_ip = outputs['dns_ip']
242 ellis_ip = outputs['ellis_ip']
243 self.config_ellis(ellis_ip)
246 vims_test_result = self.run_clearwater_live_test(
248 public_domain="") # self.inputs["public_domain"]
249 if vims_test_result != '':
250 return {'status': 'PASS', 'result': vims_test_result}
252 return {'status': 'FAIL', 'result': ''}
255 self.vnf['object'].undeploy_vnf()
256 self.orchestrator['object'].undeploy_manager()
257 super(CloudifyIms, self).clean()
260 # ----------------------------------------------------------
264 # -----------------------------------------------------------
265 def get_config(parameter, file):
267 Returns the value of a given parameter in file.yaml
268 parameter must be given in string format with dots
269 Example: general.openstack.image_name
271 with open(file) as f:
272 file_yaml = yaml.safe_load(f)
275 for element in parameter.split("."):
276 value = value.get(element)
278 raise ValueError("The parameter %s is not defined in"
279 " reporting.yaml" % parameter)