Fix the opera ims instance initialization error
[functest.git] / functest / opnfv_tests / vnf / ims / opera_ims.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2017 HUAWEI TECHNOLOGIES CO.,LTD 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 time
14
15 from opera import openo_connect
16 import requests
17
18 import functest.opnfv_tests.vnf.ims.clearwater_ims_base as clearwater_ims_base
19
20
21 class OperaIms(clearwater_ims_base.ClearwaterOnBoardingBase):
22
23     def __init__(self, **kwargs):
24         if "case_name" not in kwargs:
25             kwargs["case_name"] = "opera_ims"
26         super(OperaIms, self).__init__(**kwargs)
27         self.logger = logging.getLogger(__name__)
28         self.ellis_file = os.path.join(self.result_dir, 'ellis.info')
29         self.live_test_file = os.path.join(self.result_dir,
30                                            'live_test_report.json')
31         try:
32             self.openo_msb_endpoint = os.environ['OPENO_MSB_ENDPOINT']
33         except KeyError:
34             raise Exception('OPENO_MSB_ENDPOINT is not specified,'
35                             ' put it as <OPEN-O ip>:<port>')
36         else:
37             self.logger.info('OPEN-O endpoint is: %s', self.openo_msb_endpoint)
38
39     def prepare(self):
40         pass
41
42     def clean(self):
43         pass
44
45     def deploy_vnf(self):
46         try:
47             openo_connect.create_service(self.openo_msb_endpoint,
48                                          'functest_opera',
49                                          'VNF for functest testing')
50         except Exception as e:
51             self.logger.error(e)
52             return {'status': 'FAIL', 'result': e}
53         else:
54             self.logger.info('vIMS deployment is kicked off')
55             return {'status': 'PASS', 'result': ''}
56
57     def dump_info(self, info_file, result):
58         with open(info_file, 'w') as f:
59             self.logger.debug('Save information to file: %s', info_file)
60             json.dump(result, f)
61
62     def test_vnf(self):
63         vnfm_ip = openo_connect.get_vnfm_ip(self.openo_msb_endpoint)
64         self.logger.info('VNFM IP: %s', vnfm_ip)
65         vnf_status_url = 'http://{0}:5000/api/v1/model/status'.format(vnfm_ip)
66         vnf_alive = False
67         retry = 40
68
69         self.logger.info('Check the VNF status')
70         while retry > 0:
71             rq = requests.get(vnf_status_url, timeout=90)
72             response = rq.json()
73             vnf_alive = response['vnf_alive']
74             msg = response['msg']
75             self.logger.info(msg)
76             if vnf_alive:
77                 break
78             self.logger.info('check again in one and half a minute...')
79             retry = retry - 1
80             time.sleep(90)
81
82         if not vnf_alive:
83             raise Exception('VNF failed to start: {0}'.format(msg))
84
85         ellis_config_url = ('http://{0}:5000/api/v1/model/ellis/configure'
86                             .format(vnfm_ip))
87         rq = requests.get(ellis_config_url, timeout=90)
88         if rq.json() and not rq.json()['ellis_ok']:
89             self.logger.error(rq.json()['data'])
90             raise Exception('Failed to configure Ellis')
91
92         self.logger.info('Get Clearwater deployment detail')
93         vnf_info_url = ('http://{0}:5000/api/v1/model/output'
94                         .format(vnfm_ip))
95         rq = requests.get(vnf_info_url, timeout=90)
96         data = rq.json()['data']
97         self.logger.info(data)
98         bono_ip = data['bono_ip']
99         ellis_ip = data['ellis_ip']
100         dns_ip = data['dns_ip']
101         result = self.config_ellis(ellis_ip, 'signup', True)
102         self.logger.debug('Ellis Result: %s', result)
103         self.dump_info(self.ellis_file, result)
104
105         if dns_ip:
106             vims_test_result = self.run_clearwater_live_test(
107                 dns_ip,
108                 'clearwater.local',
109                 bono_ip,
110                 ellis_ip,
111                 'signup')
112             if vims_test_result != '':
113                 self.dump_info(self.live_test_file, vims_test_result)
114                 return {'status': 'PASS', 'result': vims_test_result}
115             else:
116                 return {'status': 'FAIL', 'result': ''}
117
118     def main(self, **kwargs):
119         self.logger.info("Start to run Opera vIMS VNF onboarding test")
120         self.execute()
121         self.logger.info("Opera vIMS VNF onboarding test finished")
122         if self.result is "PASS":
123             return self.EX_OK
124         else:
125             return self.EX_RUN_ERROR
126
127     def run(self):
128         kwargs = {}
129         return self.main(**kwargs)