Add case_name as constructor arg
[functest.git] / functest / opnfv_tests / vnf / ims / orchestra_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 json
11 import socket
12 import sys
13 import time
14 import yaml
15
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
20 import os
21 from functest.utils.constants import CONST
22
23 from org.openbaton.cli.agents.agents import MainAgent
24 from org.openbaton.cli.errors.errors import NfvoException
25
26 # ----------------------------------------------------------
27 #
28 #               UTILS
29 #
30 # -----------------------------------------------------------
31
32
33 def get_config(parameter, file):
34     """
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
38     """
39     with open(file) as f:
40         file_yaml = yaml.safe_load(f)
41     f.close()
42     value = file_yaml
43     for element in parameter.split("."):
44         value = value.get(element)
45         if value is None:
46             raise ValueError("The parameter %s is not defined in"
47                              " %s" % (parameter, file))
48     return value
49
50
51 def download_and_add_image_on_glance(glance, image_name,
52                                      image_url, data_dir):
53     dest_path = data_dir
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):
58         return False
59     image = os_utils.create_glance_image(
60         glance, image_name, dest_path + file_name)
61     if not image:
62         return False
63     return image
64
65
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)
70         try:
71             s.connect(sockaddr)
72         except socket.error:
73             return False
74         else:
75             s.close()
76             return True
77
78
79 class ImsVnf(vnf_base.VnfOnBoardingBase):
80
81     def __init__(self, project='functest', case_name='orchestra_ims',
82                  repo='', cmd=''):
83         super(ImsVnf, self).__init__(project, case_name, repo, cmd)
84         self.ob_password = "openbaton"
85         self.ob_username = "admin"
86         self.ob_https = False
87         self.ob_port = "8080"
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()
96         self.ob_nsr_id = ""
97         self.nsr = None
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
103         try:
104             self.config = CONST.__getattribute__(
105                 'vnf_{}_config'.format(self.case_name))
106         except:
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",
111                                          config_file)
112         self.bootstrap_config_link = get_config(
113             "openbaton.bootstrap_config_link", config_file)
114         self.market_link = get_config("openbaton.marketplace_link",
115                                       config_file)
116         self.images = get_config("tenant_images", config_file)
117         self.ims_conf = get_config("vIMS", config_file)
118
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()
124
125         # Import images if needed
126         # needs some images
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))
131             try:
132                 image_id = os_utils.get_image_id(glance_client,
133                                                  image_name)
134                 self.logger.info("image_id: %s" % image_id)
135             except:
136                 self.logger.error("Unexpected error: %s" % sys.exc_info()[0])
137
138             if image_id == '':
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,
142                                                             image_name,
143                                                             image_url,
144                                                             temp_dir)
145             if image_id == '':
146                 self.step_failure(
147                     "Failed to find or upload required OS "
148                     "image for this deployment")
149         network_dic = os_utils.create_network_full(neutron_client,
150                                                    "openbaton_mgmt",
151                                                    "openbaton_mgmt_subnet",
152                                                    "openbaton_router",
153                                                    "192.168.100.0/24")
154
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(
158             "orchestra",
159             "4096",
160             '20',
161             '2',
162             public=True)
163         self.logger.debug("Flavor id: %s" % flavor_id)
164
165         if not network_dic:
166             self.logger.error("There has been a problem when creating the "
167                               "neutron network")
168
169         network_id = network_dic["net_id"]
170
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']
174
175         if floatip is None:
176             self.logger.error("Cannot create floating IP.")
177
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"
195                      "horized_keys\n")
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"
204                      % floatip)
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"
220
221         sg_id = os_utils.create_security_group_full(neutron_client,
222                                                     "orchestra-sec-group",
223                                                     "allowall")
224
225         os_utils.create_secgroup_rule(neutron_client, sg_id, "ingress",
226                                       "icmp", 0, 255)
227         os_utils.create_secgroup_rule(neutron_client, sg_id, "egress",
228                                       "icmp", 0, 255)
229         os_utils.create_secgroup_rule(neutron_client, sg_id, "ingress",
230                                       "tcp", 1, 65535)
231         os_utils.create_secgroup_rule(neutron_client, sg_id, "ingress",
232                                       "udp", 1, 65535)
233         os_utils.create_secgroup_rule(neutron_client, sg_id, "egress",
234                                       "tcp", 1, 65535)
235         os_utils.create_secgroup_rule(neutron_client, sg_id, "egress",
236                                       "udp", 1, 65535)
237
238         self.logger.info("Security group set")
239
240         self.logger.info("Create instance....")
241         self.logger.info("flavor: m1.medium\n"
242                          "image: %s\n"
243                          "network_id: %s\n"
244                          "userdata: %s\n"
245                          % (self.imagename, network_id, userdata))
246
247         instance = os_utils.create_instance_and_wait_for_active(
248             "orchestra",
249             os_utils.get_image_id(glance_client, self.imagename),
250             network_id,
251             "orchestra-openbaton",
252             config_drive=False,
253             userdata=userdata)
254
255         self.ob_instance_id = instance.id
256
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)
260
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.")
265
266         self.logger.info("Waiting for Open Baton NFVO to be up and running...")
267         x = 0
268         while x < 100:
269             if servertest(floatip, "8080"):
270                 break
271             else:
272                 self.logger.debug(
273                     "Open Baton NFVO is not started yet (%ss)" %
274                     (x * 5))
275                 time.sleep(5)
276                 x += 1
277
278         if x == 100:
279             self.step_failure("Open Baton is not started correctly")
280
281         self.ob_ip = floatip
282         self.ob_password = "openbaton"
283         self.ob_username = "admin"
284         self.ob_https = False
285         self.ob_port = "8080"
286
287         self.logger.info("Deploy Open Baton NFVO: OK")
288
289     def deploy_vnf(self):
290         self.logger.info("Starting vIMS Deployment...")
291
292         self.main_agent = MainAgent(nfvo_ip=self.ob_ip,
293                                     nfvo_port=self.ob_port,
294                                     https=self.ob_https,
295                                     version=1,
296                                     username=self.ob_username,
297                                     password=self.ob_password)
298
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)
305                 break
306
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!")
310
311         creds = os_utils.get_credentials()
312         self.logger.info("PoP creds: %s" % creds)
313
314         project_id = os_utils.get_tenant_id(
315             os_utils.get_keystone_client(),
316             creds.get("project_name"))
317
318         self.logger.debug("project id: %s" % project_id)
319
320         vim_json = {
321             "name": "vim-instance",
322             "authUrl": creds.get("auth_url"),
323             "tenant": project_id,
324             "username": creds.get("username"),
325             "password": creds.get("password"),
326             "securityGroups": [
327                 "default",
328                 "orchestra-sec-group"
329             ],
330             "type": "openstack",
331             "location": {
332                 "name": "opnfv",
333                 "latitude": "52.525876",
334                 "longitude": "13.314400"
335             }
336         }
337
338         self.logger.debug("Registering VIM: %s" % vim_json)
339
340         self.main_agent.get_agent(
341             "vim",
342             project_id=self.ob_projectid).create(entity=json.dumps(vim_json))
343
344         market_agent = self.main_agent.get_agent("market",
345                                                  project_id=self.ob_projectid)
346
347         nsd = {}
348         try:
349             self.logger.info("sending: %s" % self.market_link)
350             nsd = market_agent.create(entity=self.market_link)
351             self.logger.info("Onboarded NSD: " + nsd.get("name"))
352         except NfvoException as e:
353             self.step_failure(e.message)
354
355         nsr_agent = self.main_agent.get_agent("nsr",
356                                               project_id=self.ob_projectid)
357         nsd_id = nsd.get('id')
358         if nsd_id is None:
359             self.step_failure("NSD not onboarded correctly")
360
361         try:
362             self.nsr = nsr_agent.create(nsd_id)
363         except NfvoException as e:
364             self.step_failure(e.message)
365
366         if self.nsr.get('code') is not None:
367             self.logger.error(
368                 "vIMS cannot be deployed: %s -> %s" %
369                 (self.nsr.get('code'), self.nsr.get('message')))
370             self.step_failure("vIMS cannot be deployed")
371
372         i = 0
373         self.logger.info("Waiting for NSR to go to ACTIVE...")
374         while self.nsr.get("status") != 'ACTIVE' and self.nsr.get(
375                 "status") != 'ERROR':
376             i += 1
377             if i == 150:
378                 self.step_failure("After %s sec the NSR did not go to ACTIVE.."
379                                   % 5 * i)
380             time.sleep(5)
381             self.nsr = json.loads(nsr_agent.find(self.nsr.get('id')))
382
383         if self.nsr.get("status") == 'ACTIVE':
384             deploy_vnf = {'status': "PASS", 'result': self.nsr}
385             self.logger.info("Deploy VNF: OK")
386         else:
387             deploy_vnf = {'status': "FAIL", 'result': self.nsr}
388             self.step_failure("Deploy VNF: ERROR")
389         self.ob_nsr_id = self.nsr.get("id")
390         self.logger.info(
391             "Sleep for 60s to ensure that all services are up and running...")
392         time.sleep(60)
393         return deploy_vnf
394
395     def test_vnf(self):
396         # Adaptations probably needed
397         # code used for cloudify_ims
398         # ruby client on jumphost calling the vIMS on the SUT
399         self.logger.info(
400             "Testing if %s works properly..." %
401             self.nsr.get('name'))
402         for vnfr in self.nsr.get('vnfr'):
403             self.logger.info(
404                 "Checking ports %s of VNF %s" %
405                 (self.ims_conf.get(
406                     vnfr.get('name')).get('ports'),
407                     vnfr.get('name')))
408             for vdu in vnfr.get('vdu'):
409                 for vnfci in vdu.get('vnfc_instance'):
410                     self.logger.debug(
411                         "Checking ports of VNFC instance %s" %
412                         vnfci.get('hostname'))
413                     for floatingIp in vnfci.get('floatingIps'):
414                         self.logger.debug(
415                             "Testing %s:%s" %
416                             (vnfci.get('hostname'), floatingIp.get('ip')))
417                         for port in self.ims_conf.get(
418                                 vnfr.get('name')).get('ports'):
419                             if servertest(floatingIp.get('ip'), port):
420                                 self.logger.info(
421                                     "VNFC instance %s is reachable at %s:%s" %
422                                     (vnfci.get('hostname'),
423                                      floatingIp.get('ip'),
424                                      port))
425                             else:
426                                 self.logger.error(
427                                     "VNFC instance %s is not reachable "
428                                     "at %s:%s" % (vnfci.get('hostname'),
429                                                   floatingIp.get('ip'), port))
430                                 self.step_failure("Test VNF: ERROR")
431         self.logger.info("Test VNF: OK")
432         return
433
434     def clean(self):
435         self.main_agent.get_agent(
436             "nsr",
437             project_id=self.ob_projectid).delete(self.ob_nsr_id)
438         time.sleep(5)
439         os_utils.delete_instance(nova_client=os_utils.get_nova_client(),
440                                  instance_id=self.ob_instance_id)
441         # TODO question is the clean removing also the VM?
442         # I think so since is goinf to remove the tenant...
443         super(ImsVnf, self).clean()
444
445     def main(self, **kwargs):
446         self.logger.info("Orchestra IMS VNF onboarding test starting")
447         self.execute()
448         self.logger.info("Orchestra IMS VNF onboarding test executed")
449         if self.criteria is "PASS":
450             return self.EX_OK
451         else:
452             return self.EX_RUN_ERROR
453
454     def run(self):
455         kwargs = {}
456         return self.main(**kwargs)
457
458
459 if __name__ == '__main__':
460     test = ImsVnf()
461     test.deploy_orchestrator()
462     test.deploy_vnf()
463     test.test_vnf()
464     test.clean()