blob: 88aeb45c3c47fc3da697014e08800d96588c5baa [file] [log] [blame]
Gary Wu9abb61c2018-09-27 10:38:50 -07001import json
2
3import docker
pkarasc7abba82018-10-22 12:22:35 +02004import time
Gary Wu9abb61c2018-09-27 10:38:50 -07005
6
7class PrhLibrary(object):
8
9 def __init__(self):
10 pass
11
12 @staticmethod
kurczewscd057062019-04-18 14:56:41 +020013 def find_log_entry(search_for):
14 print (type(search_for))
Gary Wu9abb61c2018-09-27 10:38:50 -070015 client = docker.from_env()
16 container = client.containers.get('prh')
Joanna Jeremicz0a391482019-03-14 10:58:32 +010017 print ("Check for log searches for pattern: ", search_for )
Gary Wu9abb61c2018-09-27 10:38:50 -070018 for line in container.logs(stream=True):
Joanna Jeremicz0a391482019-03-14 10:58:32 +010019 print ("Check for log analysis line: ", line )
Gary Wu9abb61c2018-09-27 10:38:50 -070020 if search_for in line.strip():
21 return True
22 else:
23 return False
24
25 @staticmethod
Joanna Jeremicz0a391482019-03-14 10:58:32 +010026 def create_invalid_notification(json_file):
grabinskdb4c4a52019-05-22 14:59:02 +020027 event = json.loads(json_file)[0]
28 correlation_id = PrhLibrary.extract_correlation_id_value(event, "correlationId")
29 ipv4 = PrhLibrary.extract_value_from_pnfRegistrationFields(event, "oamV4IpAddress", "oamV4IpAddress")
30 ipv6 = PrhLibrary.extract_value_from_pnfRegistrationFields(event, "oamV6IpAddress", "oamV6IpAddress")
31 serial_number = PrhLibrary.extract_value_from_pnfRegistrationFields(event, "serialNumber", "serialNumber")
32 vendor_name = PrhLibrary.extract_value_from_pnfRegistrationFields(event, "vendorName", "vendorName")
33 model_number = PrhLibrary.extract_value_from_pnfRegistrationFields(event, "modelNumber", "modelNumber")
34 unit_type = PrhLibrary.extract_value_from_pnfRegistrationFields(event, "unitType", "unitType")
Joanna Jeremicz469a3ef2019-03-19 09:37:59 +010035
grabinskdb4c4a52019-05-22 14:59:02 +020036 additional_fields = PrhLibrary.extract_additional_fields(event)
Marcin Migdal183c8962019-02-13 15:43:12 +010037
Joanna Jeremicz0a391482019-03-14 10:58:32 +010038 str_json = '{' + correlation_id + ipv4 + ipv6 + serial_number + vendor_name + model_number + unit_type + '"nfNamingCode":""' + "," + '"softwareVersion":"",' + additional_fields
39 return json.dumps(str_json).replace("\\", "")[1:-1].replace("\":", "\": ").rstrip(',') + '\\n}'
Gary Wu9abb61c2018-09-27 10:38:50 -070040
41 @staticmethod
Marcin Migdal183c8962019-02-13 15:43:12 +010042 def create_pnf_ready_notification_as_pnf_ready(json_file):
43 json_to_python = json.loads(json_file)
Joanna Jeremicz0a391482019-03-14 10:58:32 +010044 correlation_id = PrhLibrary.extract_correlation_id_value(json_to_python, "correlationId")
Joanna Jeremicz469a3ef2019-03-19 09:37:59 +010045
46 additional_fields = PrhLibrary.extract_additional_fields_value(json_to_python)
Marcin Migdal183c8962019-02-13 15:43:12 +010047
Michal Kabaj1bc046c2019-03-27 14:33:14 +010048 str_json = '{' + correlation_id + additional_fields
Joanna Jeremicz0a391482019-03-14 10:58:32 +010049
50 return json.dumps(str_json.rstrip(',') + '}').replace("\\", "")[1:-1]
Marcin Migdal183c8962019-02-13 15:43:12 +010051
52 @staticmethod
Joanna Jeremicz469a3ef2019-03-19 09:37:59 +010053 def extract_additional_fields_value(content):
54 fields = PrhLibrary.get_additional_fields_as_key_value_pairs(content)
55 if len(fields) == 0:
56 return ""
57 return PrhLibrary.build_additional_fields_json(fields)
Marcin Migdal183c8962019-02-13 15:43:12 +010058
59 @staticmethod
Joanna Jeremicz469a3ef2019-03-19 09:37:59 +010060 def extract_additional_fields(content):
61 fields = PrhLibrary.get_additional_fields_as_key_value_pairs(content)
62 if fields == []:
63 return '"additionalFields":null'
64 return PrhLibrary.build_additional_fields_json(fields)
65
66 @staticmethod
67 def get_additional_fields_as_key_value_pairs(content):
68 return content.get("event").get("pnfRegistrationFields").get(
69 "additionalFields") if "additionalFields" in content["event"]["pnfRegistrationFields"] else []
70
71 @staticmethod
72 def build_additional_fields_json(fields):
73 res = '"additionalFields":{'
Joanna Jeremicz0a391482019-03-14 10:58:32 +010074 for f in fields:
Joanna Jeremicz469a3ef2019-03-19 09:37:59 +010075 res += '"' + f + '":"' + fields.get(f) + '",'
Joanna Jeremicz0a391482019-03-14 10:58:32 +010076 return res.rstrip(',') + '},'
Marcin Migdal183c8962019-02-13 15:43:12 +010077
78 @staticmethod
Joanna Jeremicz0a391482019-03-14 10:58:32 +010079 def extract_value_from_pnfRegistrationFields(content, name, key):
80 return '"' + name + '":"' + (content.get("event").get("pnfRegistrationFields").get(key) + '",' if key in content["event"]["pnfRegistrationFields"] else '",')
Marcin Migdal183c8962019-02-13 15:43:12 +010081
82 @staticmethod
Joanna Jeremicz0a391482019-03-14 10:58:32 +010083 def extract_correlation_id_value(content, name):
84 return '"' + name + '":"' + (content.get("event").get("commonEventHeader").get("sourceName") + '",' if "sourceName" in content["event"]["commonEventHeader"] else '",')
Marcin Migdal183c8962019-02-13 15:43:12 +010085
86 @staticmethod
Gary Wu9abb61c2018-09-27 10:38:50 -070087 def create_pnf_name(json_file):
88 json_to_python = json.loads(json_file)
Marcin Migdalb3c48562019-04-05 14:37:51 +020089 correlation_id = json_to_python.get("event").get("commonEventHeader").get("sourceName") + '",' if "sourceName" in json_to_python["event"]["commonEventHeader"] else '",'
Mariusz Wagner1fd732a2018-09-28 09:03:08 +020090 return correlation_id
Gary Wu9abb61c2018-09-27 10:38:50 -070091
92 @staticmethod
pkarasc7abba82018-10-22 12:22:35 +020093 def ensure_container_is_running(name):
Gary Wu9abb61c2018-09-27 10:38:50 -070094 client = docker.from_env()
pkarasc7abba82018-10-22 12:22:35 +020095
96 if not PrhLibrary.is_in_status(client, name, "running"):
97 print ("starting container", name)
98 container = client.containers.get(name)
99 container.start()
100 PrhLibrary.wait_for_status(client, name, "running")
101
102 PrhLibrary.print_status(client)
103
104 @staticmethod
105 def ensure_container_is_exited(name):
106 client = docker.from_env()
107
108 if not PrhLibrary.is_in_status(client, name, "exited"):
109 print ("stopping container", name)
110 container = client.containers.get(name)
111 container.stop()
112 PrhLibrary.wait_for_status(client, name, "exited")
113
114 PrhLibrary.print_status(client)
115
116 @staticmethod
117 def print_status(client):
118 print("containers status")
119 for c in client.containers.list(all=True):
120 print(c.name, " ", c.status)
121
122 @staticmethod
123 def wait_for_status(client, name, status):
124 while not PrhLibrary.is_in_status(client, name, status):
125 print ("waiting for container: ", name, "to be in status: ", status)
126 time.sleep(3)
127
128 @staticmethod
129 def is_in_status(client, name, status):
kurczewscd057062019-04-18 14:56:41 +0200130 return len(client.containers.list(all=True, filters={"name": "^/"+name+"$", "status": status})) == 1