Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 1 | import argparse |
| 2 | |
| 3 | import yaml |
| 4 | |
| 5 | |
| 6 | def _find_aai_response_content(inp): |
| 7 | return inp.split("||||")[1].split("with response content = ")[1] |
| 8 | |
| 9 | def _find_openstack_url(inp): |
| 10 | return inp.split("||||")[1].split("making request with URI:")[1] |
| 11 | |
| 12 | def _find_openstack_response_content(inp): |
| 13 | return inp.split("||||")[1].split("with content:")[1].replace("u'", "'") |
| 14 | |
| 15 | def _add_response(all_responses, url, http_verb, body, status_code=200, content_type="application/json"): |
| 16 | if url not in all_responses.keys(): |
| 17 | all_responses[url] = { |
| 18 | http_verb: { |
| 19 | "status_code": status_code, |
| 20 | "content_type": content_type, |
| 21 | "body": body |
| 22 | } |
| 23 | } |
| 24 | elif http_verb not in all_responses[url].keys(): |
| 25 | all_responses[url][http_verb] = { |
| 26 | "status_code": status_code, |
| 27 | "content_type": content_type, |
| 28 | "body": body |
| 29 | } |
| 30 | |
| 31 | def parse_lines(content, aai_ip): |
| 32 | aai_pattern = "https://%s:30233/" % aai_ip |
| 33 | openstack_pattern = "making request with URI:" |
| 34 | |
| 35 | openstack_responses = {} |
| 36 | aai_responses = {} |
| 37 | for i, line in enumerate(content): |
| 38 | current_line = line.strip() |
| 39 | if aai_pattern in current_line and "DEBUG" not in current_line: |
| 40 | url = current_line.split(" ")[8][:-1].replace(aai_pattern, "") |
| 41 | _add_response(aai_responses, url, current_line.split(" ")[9][:-1], |
| 42 | _find_aai_response_content(content[i + 3])) |
| 43 | elif openstack_pattern in current_line: |
| 44 | _add_response(openstack_responses, |
| 45 | _find_openstack_url(current_line), "get", |
| 46 | _find_openstack_response_content(content[i + 2])) |
| 47 | |
| 48 | return [ |
| 49 | { "file": "nova.yml", "responses": openstack_responses }, |
| 50 | { "file": "aai.yml", "responses": aai_responses } |
| 51 | ] |
| 52 | |
| 53 | if __name__ == "__main__": |
| 54 | parser = argparse.ArgumentParser(description='Convert logs to responses YAML tree file.') |
| 55 | parser.add_argument('--log-file', type=argparse.FileType('r'), help="Log file to be parsed", required=True) |
| 56 | parser.add_argument('--aai-ip', help="A&AI IP Address", required=True) |
| 57 | args = parser.parse_args() |
| 58 | |
| 59 | for mock_responses in parse_lines(args.log_file.readlines(), args.aai_ip): |
| 60 | with open(mock_responses["file"], 'w') as yaml_file: |
| 61 | yaml.dump(mock_responses["responses"], yaml_file, default_flow_style=False) |