Merge "New python script to check openstack deployment"
[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 logging
12 import os
13 import pkg_resources
14 import socket
15 import sys
16 import time
17 import yaml
18
19 import functest.core.vnf as vnf
20 import functest.utils.openstack_utils as os_utils
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 __author__ = "Pauls, Michael <michael.pauls@fokus.fraunhofer.de>"
28 # ----------------------------------------------------------
29 #
30 #               UTILS
31 #
32 # -----------------------------------------------------------
33
34
35 def get_config(parameter, my_file):
36     """
37     Returns the value of a given parameter in file.yaml
38     parameter must be given in string format with dots
39     Example: general.openstack.image_name
40     """
41     with open(file) as f:
42         file_yaml = yaml.safe_load(f)
43     f.close()
44     value = file_yaml
45     for element in parameter.split("."):
46         value = value.get(element)
47         if value is None:
48             raise ValueError("The parameter %s is not defined in"
49                              " %s" % (parameter, my_file))
50     return value
51
52
53 def servertest(host, port):
54     args = socket.getaddrinfo(host, port, socket.AF_INET, socket.SOCK_STREAM)
55     for family, socktype, proto, canonname, sockaddr in args:
56         s = socket.socket(family, socktype, proto)
57         try:
58             s.connect(sockaddr)
59         except socket.error:
60             return False
61         else:
62             s.close()
63             return True
64
65
66 class ImsVnf(vnf.VnfOnBoarding):
67     """OpenIMS VNF deployed with openBaton orchestrator"""
68
69     def __init__(self, project='functest', case_name='orchestra_ims',
70                  repo='', cmd=''):
71         super(ImsVnf, self).__init__(project, case_name, repo, cmd)
72         self.logger = logging.getLogger(__name__)
73         self.logger.info("Orchestra IMS VNF onboarding test starting")
74         self.ob_password = "openbaton"
75         self.ob_username = "admin"
76         self.ob_https = False
77         self.ob_port = "8080"
78         self.ob_ip = "localhost"
79         self.ob_instance_id = ""
80         self.case_dir = pkg_resources.resource_filename(
81             'functest', 'opnfv_tests/vnf/ims/')
82         self.data_dir = CONST.__getattribute__('dir_ims_data')
83         self.test_dir = CONST.__getattribute__('dir_repo_vims_test')
84         self.ob_projectid = ""
85         self.keystone_client = os_utils.get_keystone_client()
86         self.ob_nsr_id = ""
87         self.nsr = None
88         self.main_agent = None
89         # vIMS Data directory creation
90         if not os.path.exists(self.data_dir):
91             os.makedirs(self.data_dir)
92         # Retrieve the configuration
93         try:
94             self.config = CONST.__getattribute__(
95                 'vnf_{}_config'.format(self.case_name))
96         except BaseException:
97             raise Exception("Orchestra VNF config file not found")
98         config_file = self.case_dir + self.config
99         self.imagename = get_config("openbaton.imagename", config_file)
100         self.bootstrap_link = get_config("openbaton.bootstrap_link",
101                                          config_file)
102         self.bootstrap_config_link = get_config(
103             "openbaton.bootstrap_config_link", config_file)
104         self.market_link = get_config("openbaton.marketplace_link",
105                                       config_file)
106         self.images = get_config("tenant_images", config_file)
107         self.ims_conf = get_config("vIMS", config_file)
108         self.userdata_file = get_config("openbaton.userdata.file",
109                                         config_file)
110
111     def deploy_orchestrator(self):
112         self.logger.info("Additional pre-configuration steps")
113         nova_client = os_utils.get_nova_client()
114         neutron_client = os_utils.get_neutron_client()
115         glance_client = os_utils.get_glance_client()
116
117         # Import images if needed
118         # needs some images
119         self.logger.info("Upload some OS images if it doesn't exist")
120         temp_dir = os.path.join(self.data_dir, "tmp/")
121         for image_name, image_url in self.images.iteritems():
122             self.logger.info("image: %s, url: %s", image_name, image_url)
123             try:
124                 image_id = os_utils.get_image_id(glance_client,
125                                                  image_name)
126                 self.logger.info("image_id: %s", image_id)
127             except BaseException:
128                 self.logger.error("Unexpected error: %s", sys.exc_info()[0])
129
130             if image_id == '':
131                 self.logger.info("""%s image doesn't exist on glance
132                                  repository. Try downloading this image
133                                  and upload on glance !""" % image_name)
134                 image_id = os_utils.download_and_add_image_on_glance(
135                     glance_client,
136                     image_name,
137                     image_url,
138                     temp_dir)
139             if image_id == '':
140                 self.logger.error("Failed to find or upload required OS "
141                                   "image for this deployment")
142                 return False
143
144         network_dic = os_utils.create_network_full(neutron_client,
145                                                    "openbaton_mgmt",
146                                                    "openbaton_mgmt_subnet",
147                                                    "openbaton_router",
148                                                    "192.168.100.0/24")
149
150         # orchestrator VM flavor
151         self.logger.info(
152             "Check if orchestra Flavor is available, if not create one")
153         flavor_exist, flavor_id = os_utils.get_or_create_flavor(
154             "orchestra",
155             "4096",
156             '20',
157             '2',
158             public=True)
159         self.logger.debug("Flavor id: %s" % flavor_id)
160
161         if not network_dic:
162             self.logger.error("There has been a problem when creating the "
163                               "neutron network")
164
165         network_id = network_dic["net_id"]
166
167         self.logger.info("Creating floating IP for VM in advance...")
168         floatip_dic = os_utils.create_floating_ip(neutron_client)
169         floatip = floatip_dic['fip_addr']
170
171         if floatip is None:
172             self.logger.error("Cannot create floating IP.")
173             return False
174
175         userdata = "#!/bin/bash\n"
176         userdata += "echo \"Executing userdata...\"\n"
177         userdata += "set -x\n"
178         userdata += "set -e\n"
179         userdata += "echo \"Set nameserver to '8.8.8.8'...\"\n"
180         userdata += "echo \"nameserver   8.8.8.8\" >> /etc/resolv.conf\n"
181         userdata += "echo \"Install curl...\"\n"
182         userdata += "apt-get install curl\n"
183         userdata += "echo \"Inject public key...\"\n"
184         userdata += ("echo \"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCuPXrV3"
185                      "geeHc6QUdyUr/1Z+yQiqLcOskiEGBiXr4z76MK4abiFmDZ18OMQlc"
186                      "fl0p3kS0WynVgyaOHwZkgy/DIoIplONVr2CKBKHtPK+Qcme2PVnCtv"
187                      "EqItl/FcD+1h5XSQGoa+A1TSGgCod/DPo+pes0piLVXP8Ph6QS1k7S"
188                      "ic7JDeRQ4oT1bXYpJ2eWBDMfxIWKZqcZRiGPgMIbJ1iEkxbpeaAd9O"
189                      "4MiM9nGCPESmed+p54uYFjwEDlAJZShcAZziiZYAvMZhvAhe6USljc"
190                      "7YAdalAnyD/jwCHuwIrUw/lxo7UdNCmaUxeobEYyyFA1YVXzpNFZya"
191                      "XPGAAYIJwEq/ openbaton@opnfv\" >> /home/ubuntu/.ssh/aut"
192                      "horized_keys\n")
193         userdata += "echo \"Download bootstrap...\"\n"
194         userdata += ("curl -s %s "
195                      "> ./bootstrap\n" % self.bootstrap_link)
196         userdata += ("curl -s %s"
197                      "> ./config_file\n" % self.bootstrap_config_link)
198         userdata += ("echo \"Disable usage of mysql...\"\n")
199         userdata += "sed -i s/mysql=.*/mysql=no/g /config_file\n"
200         userdata += ("echo \"Setting 'rabbitmq_broker_ip' to '%s'\"\n"
201                      % floatip)
202         userdata += ("sed -i s/rabbitmq_broker_ip=localhost/rabbitmq_broker_ip"
203                      "=%s/g /config_file\n" % floatip)
204         userdata += "echo \"Set autostart of components to 'false'\"\n"
205         userdata += "export OPENBATON_COMPONENT_AUTOSTART=false\n"
206         userdata += "echo \"Execute bootstrap...\"\n"
207         bootstrap = "sh ./bootstrap release -configFile=./config_file"
208         userdata += bootstrap + "\n"
209         userdata += "echo \"Setting 'nfvo.plugin.timeout' to '300000'\"\n"
210         userdata += ("echo \"nfvo.plugin.timeout=600000\" >> "
211                      "/etc/openbaton/openbaton-nfvo.properties\n")
212         userdata += (
213             "wget %s -O /etc/openbaton/openbaton-vnfm-generic-user-data.sh\n" %
214             self.userdata_file)
215         userdata += "sed -i '113i\ \ \ \ sleep 60' " \
216                     "/etc/openbaton/openbaton-vnfm-generic-user-data.sh\n"
217         userdata += "echo \"Starting NFVO\"\n"
218         userdata += "service openbaton-nfvo restart\n"
219         userdata += "echo \"Starting Generic VNFM\"\n"
220         userdata += "service openbaton-vnfm-generic restart\n"
221         userdata += "echo \"...end of userdata...\"\n"
222
223         sg_id = os_utils.create_security_group_full(neutron_client,
224                                                     "orchestra-sec-group",
225                                                     "allowall")
226
227         os_utils.create_secgroup_rule(neutron_client, sg_id, "ingress",
228                                       "icmp", 0, 255)
229         os_utils.create_secgroup_rule(neutron_client, sg_id, "egress",
230                                       "icmp", 0, 255)
231         os_utils.create_secgroup_rule(neutron_client, sg_id, "ingress",
232                                       "tcp", 1, 65535)
233         os_utils.create_secgroup_rule(neutron_client, sg_id, "ingress",
234                                       "udp", 1, 65535)
235         os_utils.create_secgroup_rule(neutron_client, sg_id, "egress",
236                                       "tcp", 1, 65535)
237         os_utils.create_secgroup_rule(neutron_client, sg_id, "egress",
238                                       "udp", 1, 65535)
239
240         self.logger.info("Security group set")
241
242         self.logger.info("Create instance....")
243         self.logger.info("flavor: m1.medium\n"
244                          "image: %s\n"
245                          "network_id: %s\n"
246                          "userdata: %s\n",
247                          self.imagename,
248                          network_id,
249                          userdata)
250
251         instance = os_utils.create_instance_and_wait_for_active(
252             "orchestra",
253             os_utils.get_image_id(glance_client, self.imagename),
254             network_id,
255             "orchestra-openbaton",
256             config_drive=False,
257             userdata=userdata)
258
259         self.ob_instance_id = instance.id
260
261         self.logger.info("Adding sec group to orchestra instance")
262         os_utils.add_secgroup_to_instance(nova_client,
263                                           self.ob_instance_id, sg_id)
264
265         self.logger.info("Associating floating ip: '%s' to VM '%s' ",
266                          floatip,
267                          "orchestra-openbaton")
268         if not os_utils.add_floating_ip(nova_client, instance.id, floatip):
269             self.logger.error("Cannot associate floating IP to VM.")
270             return False
271
272         self.logger.info("Waiting for Open Baton NFVO to be up and running...")
273         x = 0
274         while x < 200:
275             if servertest(floatip, "8080"):
276                 break
277             else:
278                 self.logger.debug(
279                     "Open Baton NFVO is not started yet (%ss)" %
280                     (x * 5))
281                 time.sleep(5)
282                 x += 1
283
284         if x == 200:
285             self.logger.error("Open Baton is not started correctly")
286
287         self.ob_ip = floatip
288         self.ob_password = "openbaton"
289         self.ob_username = "admin"
290         self.ob_https = False
291         self.ob_port = "8080"
292         self.logger.info("Waiting for all components up and running...")
293         time.sleep(60)
294         self.details["orchestrator"] = {
295             'status': "PASS", 'result': "Deploy Open Baton NFVO: OK"}
296         self.logger.info("Deploy Open Baton NFVO: OK")
297         return True
298
299     def deploy_vnf(self):
300         self.logger.info("Starting vIMS Deployment...")
301
302         self.main_agent = MainAgent(nfvo_ip=self.ob_ip,
303                                     nfvo_port=self.ob_port,
304                                     https=self.ob_https,
305                                     version=1,
306                                     username=self.ob_username,
307                                     password=self.ob_password)
308
309         self.logger.info(
310             "Check if openims Flavor is available, if not create one")
311         flavor_exist, flavor_id = os_utils.get_or_create_flavor(
312             "m1.small",
313             "2048",
314             '20',
315             '1',
316             public=True)
317         self.logger.debug("Flavor id: %s", flavor_id)
318
319         self.logger.info("Getting project 'default'...")
320         project_agent = self.main_agent.get_agent("project", self.ob_projectid)
321         for p in json.loads(project_agent.find()):
322             if p.get("name") == "default":
323                 self.ob_projectid = p.get("id")
324                 self.logger.info("Found project 'default': %s", p)
325                 break
326
327         self.logger.debug("project id: %s", self.ob_projectid)
328         if self.ob_projectid == "":
329             self.logger.error("Default project id was not found!")
330
331         creds = os_utils.get_credentials()
332         self.logger.info("PoP creds: %s", creds)
333
334         if os_utils.is_keystone_v3():
335             self.logger.info(
336                 "Using v3 API of OpenStack... -> Using OS_PROJECT_ID")
337             project_id = os_utils.get_tenant_id(
338                 os_utils.get_keystone_client(),
339                 creds.get("project_name"))
340         else:
341             self.logger.info(
342                 "Using v2 API of OpenStack... -> Using OS_TENANT_NAME")
343             project_id = creds.get("tenant_name")
344
345         self.logger.debug("project id: %s", project_id)
346
347         vim_json = {
348             "name": "vim-instance",
349             "authUrl": creds.get("auth_url"),
350             "tenant": project_id,
351             "username": creds.get("username"),
352             "password": creds.get("password"),
353             "securityGroups": [
354                 "default",
355                 "orchestra-sec-group"
356             ],
357             "type": "openstack",
358             "location": {
359                 "name": "opnfv",
360                 "latitude": "52.525876",
361                 "longitude": "13.314400"
362             }
363         }
364
365         self.logger.debug("Registering VIM: %s", vim_json)
366
367         self.main_agent.get_agent(
368             "vim",
369             project_id=self.ob_projectid).create(entity=json.dumps(vim_json))
370
371         market_agent = self.main_agent.get_agent("market",
372                                                  project_id=self.ob_projectid)
373
374         nsd = {}
375         try:
376             self.logger.info("sending: %s", self.market_link)
377             nsd = market_agent.create(entity=self.market_link)
378             self.logger.info("Onboarded NSD: " + nsd.get("name"))
379         except NfvoException as e:
380             self.logger.error(e.message)
381
382         nsr_agent = self.main_agent.get_agent("nsr",
383                                               project_id=self.ob_projectid)
384         nsd_id = nsd.get('id')
385         if nsd_id is None:
386             self.logger.error("NSD not onboarded correctly")
387
388         try:
389             self.nsr = nsr_agent.create(nsd_id)
390         except NfvoException as e:
391             self.logger.error(e.message)
392
393         if self.nsr.get('code') is not None:
394             self.logger.error(
395                 "vIMS cannot be deployed: %s -> %s",
396                 self.nsr.get('code'),
397                 self.nsr.get('message'))
398             self.logger.error("vIMS cannot be deployed")
399
400         i = 0
401         self.logger.info("Waiting for NSR to go to ACTIVE...")
402         while self.nsr.get("status") != 'ACTIVE' and self.nsr.get(
403                 "status") != 'ERROR':
404             i += 1
405             if i == 150:
406                 self.logger.error("INACTIVE NSR after %s sec..", 5 * i)
407
408             time.sleep(5)
409             self.nsr = json.loads(nsr_agent.find(self.nsr.get('id')))
410
411         if self.nsr.get("status") == 'ACTIVE':
412             self.details["vnf"] = {'status': "PASS", 'result': self.nsr}
413             self.logger.info("Deploy VNF: OK")
414         else:
415             self.details["vnf"] = {'status': "FAIL", 'result': self.nsr}
416             self.logger.error(self.nsr)
417             self.logger.error("Deploy VNF: ERROR")
418             return False
419
420         self.ob_nsr_id = self.nsr.get("id")
421         self.logger.info(
422             "Sleep for 60s to ensure that all services are up and running...")
423         time.sleep(60)
424         return True
425
426     def test_vnf(self):
427         # Adaptations probably needed
428         # code used for cloudify_ims
429         # ruby client on jumphost calling the vIMS on the SUT
430         self.logger.info(
431             "Testing if %s works properly...", self.nsr.get('name'))
432         for vnfr in self.nsr.get('vnfr'):
433             self.logger.info(
434                 "Checking ports %s of VNF %s",
435                 self.ims_conf.get(vnfr.get('name')).get('ports'),
436                 vnfr.get('name'))
437             for vdu in vnfr.get('vdu'):
438                 for vnfci in vdu.get('vnfc_instance'):
439                     self.logger.debug(
440                         "Checking ports of VNFC instance %s",
441                         vnfci.get('hostname'))
442                     for floatingIp in vnfci.get('floatingIps'):
443                         self.logger.debug(
444                             "Testing %s:%s",
445                             vnfci.get('hostname'),
446                             floatingIp.get('ip'))
447                         for port in self.ims_conf.get(
448                                 vnfr.get('name')).get('ports'):
449                             if servertest(floatingIp.get('ip'), port):
450                                 self.logger.info(
451                                     "VNFC instance %s is reachable at %s:%s",
452                                     vnfci.get('hostname'),
453                                     floatingIp.get('ip'),
454                                     port)
455                             else:
456                                 self.logger.error(
457                                     "VNFC instance %s is not reachable "
458                                     "at %s:%s",
459                                     vnfci.get('hostname'),
460                                     floatingIp.get('ip'),
461                                     port)
462                                 self.details["test_vnf"] = {
463                                     'status': "FAIL", 'result': (
464                                         "Port %s of server %s -> %s is "
465                                         "not reachable",
466                                         port,
467                                         vnfci.get('hostname'),
468                                         floatingIp.get('ip'))}
469                                 self.logger.error("Test VNF: ERROR")
470                                 return False
471
472         self.details["test_vnf"] = {
473             'status': "PASS",
474             'result': "All tests have been executed successfully"}
475         self.logger.info("Test VNF: OK")
476         return True
477
478     def clean(self):
479         self.main_agent.get_agent(
480             "nsr",
481             project_id=self.ob_projectid).delete(self.ob_nsr_id)
482         time.sleep(5)
483         os_utils.delete_instance(nova_client=os_utils.get_nova_client(),
484                                  instance_id=self.ob_instance_id)
485         # question is the clean removing also the VM?
486         # I think so since is goinf to remove the tenant...
487         super(ImsVnf, self).clean()