Merge "Stop modifying unix perm in cloudify_ims"
[functest.git] / functest / opnfv_tests / vnf / ims / cloudify_ims.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016 Orange and others.
4 #
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
9
10 import logging
11 import os
12 import sys
13
14 import requests
15 import yaml
16
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
23
24 __author__ = "Valentin Boucher <valentin.boucher@orange.com>"
25
26
27 class CloudifyIms(clearwater_ims_base.ClearwaterOnBoardingBase):
28     """Clearwater vIMS deployed with Cloudify Orchestrator Case"""
29
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 = ''
38         self.nova_client = ''
39
40         # Retrieve the configuration
41         try:
42             self.config = CONST.__getattribute__(
43                 'vnf_{}_config'.format(self.case_name))
44         except Exception:
45             raise Exception("VNF config file not found")
46
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)
52         )
53         self.logger.debug("Orchestrator configuration: %s", self.orchestrator)
54         self.vnf = dict(
55             blueprint=get_config("clearwater.blueprint", config_file),
56             deployment_name=get_config("clearwater.deployment_name",
57                                        config_file),
58             inputs=get_config("clearwater.inputs", config_file),
59             requirements=get_config("clearwater.requirements", config_file)
60         )
61         self.logger.debug("VNF configuration: %s", self.vnf)
62
63         self.images = get_config("tenant_images", config_file)
64         self.logger.info("Images needed for vIMS: %s", self.images)
65
66     def deploy_orchestrator(self):
67
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)
73
74         # needs some images
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)
79             try:
80                 image_id = os_utils.get_image_id(self.glance_client,
81                                                  image_name)
82                 self.logger.debug("image_id: %s", image_id)
83             except Exception:
84                 self.logger.error("Unexpected error: %s", sys.exc_info()[0])
85
86             if image_id == '':
87                 self.logger.info("""%s image does not exist on glance repo.
88                                  Try downloading this image
89                                  and upload on glance !""",
90                                  image_name)
91                 image_id = os_utils.download_and_add_image_on_glance(
92                     self.glance_client,
93                     image_name,
94                     image_url,
95                     temp_dir)
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,
99                                            self.tenant_name)
100         self.logger.debug("Tenant id found %s", tenant_id)
101         if not os_utils.update_sg_quota(self.neutron_client,
102                                         tenant_id, 50, 100):
103             self.logger.error("Failed to update security group quota"
104                               " for tenant " + self.tenant_name)
105
106         self.logger.debug("group quota extended")
107
108         # start the deployment of cloudify
109         public_auth_url = os_utils.get_endpoint('identity')
110
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")
115
116         self.logger.debug("Tenant name: %s", self.tenant_name)
117
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")
123
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(
129             "m1.large",
130             self.orchestrator['requirements']['ram_min'],
131             '50',
132             '2',
133             public=True)
134         self.logger.debug("Flavor id: %s", flavor_id)
135
136         if not flavor_id:
137             self.logger.info("Available flavors are: ")
138             self.logger.info(self.nova_client.flavor.list())
139
140         cfy.set_flavor_id(flavor_id)
141         self.logger.debug("Flavor OK")
142
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(
147                 self.glance_client,
148                 self.orchestrator['requirements']['os_image'])
149             self.logger.debug("Orchestrator image id: %s", image_id)
150             if image_id == '':
151                 self.logger.error("CFY image not found")
152
153         cfy.set_image_id(image_id)
154         self.logger.debug("Orchestrator image set")
155
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)
159
160         cfy.set_external_network_name(ext_net)
161         self.logger.debug("CFY External network set")
162
163         self.logger.debug("get resolvconf")
164         ns = ft_utils.get_resolvconf_ns()
165         if ns:
166             cfy.set_nameservers(ns)
167             self.logger.debug("Resolvconf set")
168
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)
173
174         cfy.download_manager_blueprint(
175             self.orchestrator['blueprint']['url'],
176             self.orchestrator['blueprint']['branch'])
177
178         error = cfy.deploy_manager()
179         if error:
180             self.logger.error(error)
181             return {'status': 'FAIL', 'result': error}
182         else:
183             return {'status': 'PASS', 'result': ''}
184
185     def deploy_vnf(self):
186         cw = Clearwater(self.vnf['inputs'], self.orchestrator['object'],
187                         self.logger)
188         self.vnf['object'] = cw
189
190         self.logger.info("Collect flavor id for all clearwater vm")
191         flavor_exist, flavor_id = os_utils.get_or_create_flavor(
192             "m1.small",
193             self.vnf['requirements']['ram_min'],
194             '30',
195             '1',
196             public=True)
197         self.logger.debug("Flavor id: %s", flavor_id)
198         if not flavor_id:
199             self.logger.info("Available flavors are: ")
200             self.logger.info(self.nova_client.flavor.list())
201
202         cw.set_flavor_id(flavor_id)
203
204         # VMs image
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'])
208
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)
212
213         error = cw.deploy_vnf(self.vnf['blueprint'])
214         if error:
215             self.logger.error(error)
216             return {'status': 'FAIL', 'result': error}
217         else:
218             return {'status': 'PASS', 'result': ''}
219
220     def test_vnf(self):
221         script = "source {0}venv_cloudify/bin/activate; "
222         script += "cd {0}; "
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) + "'"
225
226         try:
227             self.logger.debug("Trying to get clearwater manager IP ... ")
228             mgr_ip = os.popen(cmd).read()
229             mgr_ip = mgr_ip.splitlines()[0]
230         except Exception:
231             self.logger.exception("Unable to retrieve the IP of the "
232                                   "cloudify manager server !")
233
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)
244
245         if dns_ip != "":
246             vims_test_result = self.run_clearwater_live_test(
247                 dns_ip=dns_ip,
248                 public_domain="")  # self.inputs["public_domain"]
249             if vims_test_result != '':
250                 return {'status': 'PASS', 'result': vims_test_result}
251             else:
252                 return {'status': 'FAIL', 'result': ''}
253
254     def clean(self):
255         self.vnf['object'].undeploy_vnf()
256         self.orchestrator['object'].undeploy_manager()
257         super(CloudifyIms, self).clean()
258
259
260 # ----------------------------------------------------------
261 #
262 #               YAML UTILS
263 #
264 # -----------------------------------------------------------
265 def get_config(parameter, file):
266     """
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
270     """
271     with open(file) as f:
272         file_yaml = yaml.safe_load(f)
273     f.close()
274     value = file_yaml
275     for element in parameter.split("."):
276         value = value.get(element)
277         if value is None:
278             raise ValueError("The parameter %s is not defined in"
279                              " reporting.yaml" % parameter)
280     return value