Add reporting to MongoDB
[bottlenecks.git] / utils / parser.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 '''This file realize the function of how to parser a config file.
11 This contain Two part:
12 Frist is Init some variables the will be used.
13 Second is reading config file.'''
14
15 import os
16 import yaml
17 import json
18 import time
19 from pyroute2 import IPDB
20
21
22 class Parser():
23
24     bottlenecks_config = {}
25     bottlenecks_test = {}
26
27     @classmethod
28     def config_init(cls):
29         cls.code_dir = os.path.dirname(os.path.abspath(__file__))
30         cls.root_dir = os.path.dirname(cls.code_dir)
31         cls.test_dir = os.path.join(cls.root_dir, 'testsuites')
32         config_dir = os.path.join(
33             cls.root_dir,
34             'config',
35             'config.yaml')
36
37         with open(config_dir) as file:
38             config_info = yaml.load(file)
39             common_config = config_info['common_config']
40             cls.bottlenecks_config["releng_dir"] = common_config["releng_dir"]
41             cls.bottlenecks_config["fetch_os"] = common_config["fetch_os_file"]
42             cls.bottlenecks_config["log_dir"] = common_config['log_dir']
43             cls.bottlenecks_config["rc_dir"] = common_config['rc_dir']
44             cls.config_dir_check(cls.bottlenecks_config["log_dir"])
45
46     @classmethod
47     def story_read(cls, testcase, story_name):
48         story_dir = os.path.join(
49             cls.test_dir,
50             testcase,
51             'testsuite_story',
52             story_name + '.yaml')
53         with open(story_dir) as file:
54             story_parser = yaml.load(file)
55         for case_name in story_parser['testcase']:
56             Parser.testcase_read(testcase, case_name)
57
58         return cls.bottlenecks_test
59
60     @classmethod
61     def testcase_read(cls, testcase, testcase_name):
62
63         testcase_dir = os.path.join(
64             cls.test_dir,
65             testcase,
66             'testcase_cfg',
67             testcase_name)
68         testcase_local = testcase_dir + ".yaml"
69         with open(testcase_local) as f:
70             cls.bottlenecks_test[testcase_name] = yaml.load(f)
71
72         return cls.bottlenecks_test
73
74     @classmethod
75     def config_dir_check(cls, dirname):
76         if dirname is None:
77             dirname = '/tmp/'
78         if not os.path.exists(dirname):
79             os.makedirs(dirname)
80
81     @classmethod
82     def testcase_out_dir(cls, testcase):
83         file_time = time.strftime('%H_%M', time.localtime(time.time()))
84         out_name = cls.bottlenecks_config["log_dir"] + testcase + file_time
85         outfile_name = out_name + ".out"
86         return outfile_name
87
88     @staticmethod
89     def config_parser(testcase_cfg, parameters):
90         test_cfg = testcase_cfg['test_config']
91         stack_cfg = testcase_cfg['stack_config']
92         # TO-DO add cli parameters to stack_config.
93         return test_cfg, stack_cfg
94
95     @staticmethod
96     def ip_parser(ip_type):
97         with IPDB() as ip:
98             GATEWAY_IP = ip.routes['default'].gateway
99         if ip_type is "dashboard":
100             TEST_IP = GATEWAY_IP + ":9200"
101         elif ip_type is "yardstick_test_ip":
102             TEST_IP = GATEWAY_IP + ":8888"
103         return TEST_IP
104
105     @staticmethod
106     def result_to_file(data, file_name):
107         with open(file_name, "a") as f:
108             f.write(json.dumps(data, f))
109             f.write("\n")
110
111
112 class HeatTemplate_Parser():
113     """parser a Heat template and a method to deploy template to a stack"""
114
115     def __init__(self):
116         self.heat_date = {}
117         self.heat_date["resources"] = {}
118         self.heat_date["outputs"] = {}
119         self.heat_date["heat_template_version"] = "2013-05-23"
120         self.heat_date["description"] = {"Stack built by the bottlenecks"
121                                          " framework for root."}
122
123     def add_security_group(self, name):
124         """add to the template a Neutron SecurityGroup"""
125         security_name = name + "-security_group"
126         self.heat_date["resources"][security_name] = {
127             "type": "OS::Neutron::SecurityGroup",
128             "properties": {
129                 "name": security_name,
130                 "description": "Group allowing icmp and upd/tcp on all ports",
131                 "rules": [
132                     {"remote_ip_prefix": "0.0.0.0/0",
133                      "protocol": "tcp",
134                      "port_range_min": "1",
135                      "port_range_max": "65535"},
136                     {"remote_ip_prefix": "0.0.0.0/0",
137                      "protocol": "udp",
138                      "port_range_min": "1",
139                      "port_range_max": "65535"},
140                     {"remote_ip_prefix": "0.0.0.0/0",
141                      "protocol": "icmp"}
142                 ]
143             }
144         }
145
146         self.heat_date["outputs"][security_name] = {
147             "description": "ID of Security Group",
148             "value": {"get_resource": security_name}
149         }
150
151     def add_keypair(self, name):
152         """add to the template a Nova KeyPair"""
153         key_name = name + "key"
154         with open(Parser.root_dir +
155                   "utils/infra_setup/"
156                   "bottlenecks_key/bottlenecks_key.pub") as f:
157             key_content = f.read()
158             self.heat_date["resources"][key_name] = {
159                 "type": "OS::Nova::KeyPair",
160                 "properties": {
161                     "name": key_name,
162                     "public_key": key_content
163                 }
164             }
165
166     def add_network(self, name):
167         """add to the template a Neutron Net"""
168         network_name = name + "-net"
169         self.heat_date["resources"][network_name] = {
170             "type": "OS::Neutron::Net",
171             "properties": {"name": network_name}
172         }
173
174     def add_subnet(self, name, cidr):
175         """add to the template a Neutron Subnet"""
176         network_name = name + "-net"
177         subnet_name = name + "-subnet"
178         self.heat_date["resources"][subnet_name] = {
179             "type": "OS::Neutron::Subnet",
180             "depends_on": network_name,
181             "properties": {
182                 "name": subnet_name,
183                 "cidr": cidr,
184                 "network_id": {"get_resource": network_name}
185             }
186         }
187
188         self.heat_date["outputs"][subnet_name] = {
189             "description": "subnet %s ID" % subnet_name,
190             "value": {"get_resource": subnet_name}
191         }
192
193     def add_router(self, name, ext_gw_net):
194         """add to the template a Neutron Router and interface"""
195         router_name = name + "-route"
196         subnet_name = name + "-subnet"
197
198         self.heat_date["resources"][router_name] = {
199             "type": "OS::Neutron::Router",
200             "depends_on": [subnet_name],
201             "properties": {
202                 "name": router_name,
203                 "external_gateway_info": {
204                     "network": ext_gw_net
205                 }
206             }
207         }
208
209     def add_router_interface(self, name):
210         """add to the template a Neutron RouterInterface and interface"""
211         router_name = name + "-route"
212         subnet_name = name + "-subnet"
213         router_if_name = name + "-interface"
214
215         self.heat_date["resources"][router_if_name] = {
216             "type": "OS::Neutron::RouterInterface",
217             "depends_on": [router_name, subnet_name],
218             "properties": {
219                 "router_id": {"get_resource": router_name},
220                 "subnet_id": {"get_resource": subnet_name}
221             }
222         }
223
224     def add_server(self, name, image, flavor, user, ports=None):
225         """add to the template a Nova Server"""
226
227         key_name = "bottlenecks-poscakey"
228         port_name = name + "-port"
229         self.heat_date["resources"][name] = {
230             "type": "OS::Nova::Server",
231             "properties": {
232                 "name": name,
233                 "flavor": flavor,
234                 "image": image,
235                 "key_name": {"get_resource": key_name},
236                 "admin_user": user,
237                 "networks": [{
238                     "port": {"get_resource": port_name}
239                 }]
240             }
241         }
242
243         self.heat_date["outputs"][name] = {
244             "description": "VM UUID",
245             "value": {"get_resource": name}
246         }
247
248     def add_port(self, name, stack_name=None):
249         """add to the template a named Neutron Port"""
250         network_name = stack_name + "-net"
251         subnet_name = stack_name + "-subnet"
252         port_name = name + "-port"
253         security_name = stack_name + "-security_group"
254
255         self.heat_date["resources"][port_name] = {
256             "type": "OS::Neutron::Port",
257             "depends_on": [subnet_name],
258             "properties": {
259                 "name": port_name,
260                 "fixed_ips": [{"subnet": {"get_resource": subnet_name}}],
261                 "network_id": {"get_resource": network_name},
262                 "replacement_policy": "AUTO",
263                 "security_groups": [{"get_resource": security_name}]
264             }
265         }
266
267         self.heat_date["outputs"][port_name] = {
268             "description": "Address for interface %s" % port_name,
269             "value": {"get_attr": [port_name, "fixed_ips", 0, "ip_address"]}
270         }
271
272     def add_floating_ip(self, name, stack_name, network_ext):
273         """add to the template a Nova FloatingIP resource
274         see: https://bugs.launchpad.net/heat/+bug/1299259
275         """
276         port_name = name + "-port"
277         floating_ip_name = name + "-floating"
278         router_if_name = stack_name + "-interface"
279
280         self.heat_date["resources"][floating_ip_name] = {
281             "depends_on": [router_if_name, port_name],
282             "type": "OS::Nova::FloatingIP",
283             "properties": {
284                 "pool": network_ext,
285             }
286         }
287         self.heat_date['outputs'][floating_ip_name] = {
288             'description': 'floating ip %s' % name,
289             'value': {'get_attr': [name, 'ip']}
290         }
291
292     def add_floating_ip_ass(self, name):
293         """add to the template a Nova FloatingIP resource
294         see: https://bugs.launchpad.net/heat/+bug/1299259
295         """
296         port_name = name + "-port"
297         floating_ip_name = name + "-floating"
298         floating_ass = name + "-floating_ass"
299
300         self.heat_date["resources"][floating_ass] = {
301             "type": 'OS::Neutron::FloatingIPAssociation',
302             "depends_on": [port_name, floating_ip_name],
303             "properties": {
304                 "floatingip_id": {'get_resource': floating_ip_name},
305                 "port_id": {"get_resource": port_name}
306             }
307         }
308
309     def get_template_date(self):
310         return self.heat_date