Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 1 | import json |
| 2 | |
| 3 | import docker |
pkaras | c7abba8 | 2018-10-22 12:22:35 +0200 | [diff] [blame] | 4 | import time |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 5 | |
| 6 | |
| 7 | class PrhLibrary(object): |
| 8 | |
| 9 | def __init__(self): |
| 10 | pass |
| 11 | |
| 12 | @staticmethod |
| 13 | def check_for_log(search_for): |
| 14 | client = docker.from_env() |
| 15 | container = client.containers.get('prh') |
Joanna Jeremicz | 0a39148 | 2019-03-14 10:58:32 +0100 | [diff] [blame] | 16 | print ("Check for log searches for pattern: ", search_for ) |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 17 | for line in container.logs(stream=True): |
Joanna Jeremicz | 0a39148 | 2019-03-14 10:58:32 +0100 | [diff] [blame] | 18 | print ("Check for log analysis line: ", line ) |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 19 | if search_for in line.strip(): |
| 20 | return True |
| 21 | else: |
| 22 | return False |
| 23 | |
| 24 | @staticmethod |
Joanna Jeremicz | 0a39148 | 2019-03-14 10:58:32 +0100 | [diff] [blame] | 25 | def create_invalid_notification(json_file): |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 26 | json_to_python = json.loads(json_file) |
Joanna Jeremicz | 0a39148 | 2019-03-14 10:58:32 +0100 | [diff] [blame] | 27 | correlation_id = PrhLibrary.extract_correlation_id_value(json_to_python, "correlationId") |
| 28 | ipv4 = PrhLibrary.extract_value_from_pnfRegistrationFields(json_to_python, "oamV4IpAddress", "oamV4IpAddress") |
| 29 | ipv6 = PrhLibrary.extract_value_from_pnfRegistrationFields(json_to_python, "oamV6IpAddress", "oamV6IpAddress") |
| 30 | serial_number = PrhLibrary.extract_value_from_pnfRegistrationFields(json_to_python, "serialNumber", "serialNumber") |
| 31 | vendor_name = PrhLibrary.extract_value_from_pnfRegistrationFields(json_to_python, "vendorName", "vendorName") |
| 32 | model_number = PrhLibrary.extract_value_from_pnfRegistrationFields(json_to_python, "modelNumber", "modelNumber") |
| 33 | unit_type = PrhLibrary.extract_value_from_pnfRegistrationFields(json_to_python, "unitType", "unitType") |
Joanna Jeremicz | 469a3ef | 2019-03-19 09:37:59 +0100 | [diff] [blame] | 34 | |
| 35 | additional_fields = PrhLibrary.extract_additional_fields(json_to_python) |
Marcin Migdal | 183c896 | 2019-02-13 15:43:12 +0100 | [diff] [blame] | 36 | |
Joanna Jeremicz | 0a39148 | 2019-03-14 10:58:32 +0100 | [diff] [blame] | 37 | str_json = '{' + correlation_id + ipv4 + ipv6 + serial_number + vendor_name + model_number + unit_type + '"nfNamingCode":""' + "," + '"softwareVersion":"",' + additional_fields |
| 38 | return json.dumps(str_json).replace("\\", "")[1:-1].replace("\":", "\": ").rstrip(',') + '\\n}' |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 39 | |
| 40 | @staticmethod |
Marcin Migdal | 183c896 | 2019-02-13 15:43:12 +0100 | [diff] [blame] | 41 | def create_pnf_ready_notification_as_pnf_ready(json_file): |
| 42 | json_to_python = json.loads(json_file) |
Joanna Jeremicz | 0a39148 | 2019-03-14 10:58:32 +0100 | [diff] [blame] | 43 | correlation_id = PrhLibrary.extract_correlation_id_value(json_to_python, "correlationId") |
Joanna Jeremicz | 469a3ef | 2019-03-19 09:37:59 +0100 | [diff] [blame] | 44 | |
| 45 | additional_fields = PrhLibrary.extract_additional_fields_value(json_to_python) |
Marcin Migdal | 183c896 | 2019-02-13 15:43:12 +0100 | [diff] [blame] | 46 | |
Michal Kabaj | 1bc046c | 2019-03-27 14:33:14 +0100 | [diff] [blame] | 47 | str_json = '{' + correlation_id + additional_fields |
Joanna Jeremicz | 0a39148 | 2019-03-14 10:58:32 +0100 | [diff] [blame] | 48 | |
| 49 | return json.dumps(str_json.rstrip(',') + '}').replace("\\", "")[1:-1] |
Marcin Migdal | 183c896 | 2019-02-13 15:43:12 +0100 | [diff] [blame] | 50 | |
| 51 | @staticmethod |
Joanna Jeremicz | 469a3ef | 2019-03-19 09:37:59 +0100 | [diff] [blame] | 52 | def extract_additional_fields_value(content): |
| 53 | fields = PrhLibrary.get_additional_fields_as_key_value_pairs(content) |
| 54 | if len(fields) == 0: |
| 55 | return "" |
| 56 | return PrhLibrary.build_additional_fields_json(fields) |
Marcin Migdal | 183c896 | 2019-02-13 15:43:12 +0100 | [diff] [blame] | 57 | |
| 58 | @staticmethod |
Joanna Jeremicz | 469a3ef | 2019-03-19 09:37:59 +0100 | [diff] [blame] | 59 | def extract_additional_fields(content): |
| 60 | fields = PrhLibrary.get_additional_fields_as_key_value_pairs(content) |
| 61 | if fields == []: |
| 62 | return '"additionalFields":null' |
| 63 | return PrhLibrary.build_additional_fields_json(fields) |
| 64 | |
| 65 | @staticmethod |
| 66 | def get_additional_fields_as_key_value_pairs(content): |
| 67 | return content.get("event").get("pnfRegistrationFields").get( |
| 68 | "additionalFields") if "additionalFields" in content["event"]["pnfRegistrationFields"] else [] |
| 69 | |
| 70 | @staticmethod |
| 71 | def build_additional_fields_json(fields): |
| 72 | res = '"additionalFields":{' |
Joanna Jeremicz | 0a39148 | 2019-03-14 10:58:32 +0100 | [diff] [blame] | 73 | for f in fields: |
Joanna Jeremicz | 469a3ef | 2019-03-19 09:37:59 +0100 | [diff] [blame] | 74 | res += '"' + f + '":"' + fields.get(f) + '",' |
Joanna Jeremicz | 0a39148 | 2019-03-14 10:58:32 +0100 | [diff] [blame] | 75 | return res.rstrip(',') + '},' |
Marcin Migdal | 183c896 | 2019-02-13 15:43:12 +0100 | [diff] [blame] | 76 | |
| 77 | @staticmethod |
Joanna Jeremicz | 0a39148 | 2019-03-14 10:58:32 +0100 | [diff] [blame] | 78 | def extract_value_from_pnfRegistrationFields(content, name, key): |
| 79 | return '"' + name + '":"' + (content.get("event").get("pnfRegistrationFields").get(key) + '",' if key in content["event"]["pnfRegistrationFields"] else '",') |
Marcin Migdal | 183c896 | 2019-02-13 15:43:12 +0100 | [diff] [blame] | 80 | |
| 81 | @staticmethod |
Joanna Jeremicz | 0a39148 | 2019-03-14 10:58:32 +0100 | [diff] [blame] | 82 | def extract_correlation_id_value(content, name): |
| 83 | return '"' + name + '":"' + (content.get("event").get("commonEventHeader").get("sourceName") + '",' if "sourceName" in content["event"]["commonEventHeader"] else '",') |
Marcin Migdal | 183c896 | 2019-02-13 15:43:12 +0100 | [diff] [blame] | 84 | |
| 85 | @staticmethod |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 86 | def create_pnf_name(json_file): |
| 87 | json_to_python = json.loads(json_file) |
Marcin Migdal | b3c4856 | 2019-04-05 14:37:51 +0200 | [diff] [blame] | 88 | correlation_id = json_to_python.get("event").get("commonEventHeader").get("sourceName") + '",' if "sourceName" in json_to_python["event"]["commonEventHeader"] else '",' |
Mariusz Wagner | 1fd732a | 2018-09-28 09:03:08 +0200 | [diff] [blame] | 89 | return correlation_id |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 90 | |
| 91 | @staticmethod |
pkaras | c7abba8 | 2018-10-22 12:22:35 +0200 | [diff] [blame] | 92 | def ensure_container_is_running(name): |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 93 | client = docker.from_env() |
pkaras | c7abba8 | 2018-10-22 12:22:35 +0200 | [diff] [blame] | 94 | |
| 95 | if not PrhLibrary.is_in_status(client, name, "running"): |
| 96 | print ("starting container", name) |
| 97 | container = client.containers.get(name) |
| 98 | container.start() |
| 99 | PrhLibrary.wait_for_status(client, name, "running") |
| 100 | |
| 101 | PrhLibrary.print_status(client) |
| 102 | |
| 103 | @staticmethod |
| 104 | def ensure_container_is_exited(name): |
| 105 | client = docker.from_env() |
| 106 | |
| 107 | if not PrhLibrary.is_in_status(client, name, "exited"): |
| 108 | print ("stopping container", name) |
| 109 | container = client.containers.get(name) |
| 110 | container.stop() |
| 111 | PrhLibrary.wait_for_status(client, name, "exited") |
| 112 | |
| 113 | PrhLibrary.print_status(client) |
| 114 | |
| 115 | @staticmethod |
| 116 | def print_status(client): |
| 117 | print("containers status") |
| 118 | for c in client.containers.list(all=True): |
| 119 | print(c.name, " ", c.status) |
| 120 | |
| 121 | @staticmethod |
| 122 | def wait_for_status(client, name, status): |
| 123 | while not PrhLibrary.is_in_status(client, name, status): |
| 124 | print ("waiting for container: ", name, "to be in status: ", status) |
| 125 | time.sleep(3) |
| 126 | |
| 127 | @staticmethod |
| 128 | def is_in_status(client, name, status): |
| 129 | return len(client.containers.list(all=True, filters={"name": "^/"+name+"$", "status": status})) == 1 |