5 from docker.utils.json_stream import json_stream
6 from collections import OrderedDict
9 class BbsLibrary(object):
15 def check_for_log(search_for):
16 client = docker.from_env()
17 container = client.containers.get('bbs')
19 alog = container.logs(stream=False, tail=1000)
22 except AttributeError:
25 found = alog.find(search_for)
32 def create_pnf_name_from_auth(json_file):
33 json_to_python = json.loads(json_file)
34 correlation_id = json_to_python.get("event").get("commonEventHeader").get("sourceName")
38 def get_invalid_auth_elements(json_file):
40 Get the correlationId, oldState, newState, stateInterface, macAddress, swVersion elements
41 from the invalid message and place the elements into a JSON object (string) as fields for comparision
43 eventString = json.loads(json_file)[0]
44 json_to_python = json.loads(eventString.replace("\\", ""))
45 correlation_id = json_to_python.get("event").get("commonEventHeader").get("sourceName")
46 oldState = json_to_python.get("event").get("stateChangeFields").get("oldState")
47 newState = json_to_python.get("event").get("stateChangeFields").get("newState")
48 stateInterface = json_to_python.get("event").get("stateChangeFields").get("stateInterface")
49 macAddress = json_to_python.get("event").get("stateChangeFields").get("additionalFields").get("macAddress")
50 swVersion = json_to_python.get("event").get("stateChangeFields").get("additionalFields").get("swVersion")
54 inv_fields = OrderedDict()
57 inv_fields['correlationId'] = correlation_id
58 inv_fields['oldState'] = oldState
59 inv_fields['newState'] = newState
60 inv_fields['stateInterface'] = stateInterface
61 inv_fields['macAddress'] = macAddress
62 inv_fields['swVersion'] = swVersion
64 # Transform the dictionary to JSON string
65 json_str = json.dumps(inv_fields)
67 # Need to remove spaces between elements
68 json_str = json_str.replace(', ', ',')
72 def get_invalid_update_elements(json_file):
74 Get the correlationId, attachment-point, remote-id, cvlan, svlan, elements
75 from the invalid message and place the elements into a JSON object (string) as fields for comparision
77 eventString = json.loads(json_file)[0]
78 json_to_python = json.loads(eventString.replace("\\", ""))
79 correlation_id = json_to_python.get("correlationId")
80 attachmentPoint = json_to_python.get("additionalFields").get("attachment-point")
81 remoteId = json_to_python.get("additionalFields").get("remote-id")
82 cvlan = json_to_python.get("additionalFields").get("cvlan")
83 svlan = json_to_python.get("additionalFields").get("svlan")
85 inv_fields = OrderedDict()
87 inv_fields['correlationId'] = correlation_id
88 inv_fields['attachment-point'] = attachmentPoint
89 inv_fields['remote-id'] = remoteId
90 inv_fields['cvlan'] = cvlan
91 inv_fields['svlan'] = svlan
93 # Transform the dictionary to JSON string
94 json_str = json.dumps(inv_fields)
96 # Need to remove spaces between elements
97 json_str = json_str.replace(', ', ',')
101 def compare_policy(dmaap_policy, json_policy):
104 python_policy = json.loads(json_policy).pop()
109 python_dmaap_policy = json.loads(dmaap_policy)
111 python_dmaap_policy = ""
114 d_policy = python_dmaap_policy[0].get("policyName")
119 j_policy = python_policy.get("policyName")
124 if (d_policy == j_policy):
129 def create_pnf_name_from_update(json_file):
130 json_to_python = json.loads(json_file)
131 correlation_id = json_to_python.get("correlationId")
132 return correlation_id
135 def ensure_container_is_running(name):
137 client = docker.from_env()
139 if not BbsLibrary.is_in_status(client, name, "running"):
140 print ("starting container", name)
141 container = client.containers.get(name)
143 BbsLibrary.wait_for_status(client, name, "running")
145 BbsLibrary.print_status(client)
148 def ensure_container_is_exited(name):
150 client = docker.from_env()
152 if not BbsLibrary.is_in_status(client, name, "exited"):
153 print ("stopping container", name)
154 container = client.containers.get(name)
156 BbsLibrary.wait_for_status(client, name, "exited")
158 BbsLibrary.print_status(client)
161 def print_status(client):
162 print("containers status")
163 for c in client.containers.list(all=True):
164 print(c.name, " ", c.status)
167 def wait_for_status(client, name, status):
168 while not BbsLibrary.is_in_status(client, name, status):
169 print ("waiting for container: ", name, "to be in status: ", status)
173 def is_in_status(client, name, status):
174 return len(client.containers.list(all=True, filters={"name": "^/"+name+"$", "status": status})) == 1