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 json_to_python = json.loads(json_file)
44 correlation_id = json_to_python.get("event").get("commonEventHeader").get("sourceName")
45 oldState = json_to_python.get("event").get("stateChangeFields").get("oldState")
46 newState = json_to_python.get("event").get("stateChangeFields").get("newState")
47 stateInterface = json_to_python.get("event").get("stateChangeFields").get("stateInterface")
48 macAddress = json_to_python.get("event").get("stateChangeFields").get("additionalFields").get("macAddress")
49 swVersion = json_to_python.get("event").get("stateChangeFields").get("additionalFields").get("swVersion")
53 inv_fields = OrderedDict()
56 inv_fields['correlationId'] = correlation_id
57 inv_fields['oldState'] = oldState
58 inv_fields['newState'] = newState
59 inv_fields['stateInterface'] = stateInterface
60 inv_fields['macAddress'] = macAddress
61 inv_fields['swVersion'] = swVersion
63 # Transform the dictionary to JSON string
64 json_str = json.dumps(inv_fields)
66 # Need to remove spaces between elements
67 json_str = json_str.replace(', ', ',')
71 def get_invalid_update_elements(json_file):
73 Get the correlationId, attachment-point, remote-id, cvlan, svlan, elements
74 from the invalid message and place the elements into a JSON object (string) as fields for comparision
76 json_to_python = json.loads(json_file)
77 correlation_id = json_to_python.get("correlationId")
78 attachmentPoint = json_to_python.get("additionalFields").get("attachment-point")
79 remoteId = json_to_python.get("additionalFields").get("remote-id")
80 cvlan = json_to_python.get("additionalFields").get("cvlan")
81 svlan = json_to_python.get("additionalFields").get("svlan")
83 inv_fields = OrderedDict()
85 inv_fields['correlationId'] = correlation_id
86 inv_fields['attachment-point'] = attachmentPoint
87 inv_fields['remote-id'] = remoteId
88 inv_fields['cvlan'] = cvlan
89 inv_fields['svlan'] = svlan
91 # Transform the dictionary to JSON string
92 json_str = json.dumps(inv_fields)
94 # Need to remove spaces between elements
95 json_str = json_str.replace(', ', ',')
99 def compare_policy(dmaap_policy, json_policy):
102 python_policy = json.loads(json_policy).pop()
107 python_dmaap_policy = json.loads(dmaap_policy)
109 python_dmaap_policy = ""
112 d_policy = python_dmaap_policy.get("policyName")
117 j_policy = python_policy.get("policyName")
122 if (d_policy == j_policy):
127 def create_pnf_name_from_update(json_file):
128 json_to_python = json.loads(json_file)
129 correlation_id = json_to_python.get("correlationId")
130 return correlation_id
133 def ensure_container_is_running(name):
135 client = docker.from_env()
137 if not BbsLibrary.is_in_status(client, name, "running"):
138 print ("starting container", name)
139 container = client.containers.get(name)
141 BbsLibrary.wait_for_status(client, name, "running")
143 BbsLibrary.print_status(client)
146 def ensure_container_is_exited(name):
148 client = docker.from_env()
150 if not BbsLibrary.is_in_status(client, name, "exited"):
151 print ("stopping container", name)
152 container = client.containers.get(name)
154 BbsLibrary.wait_for_status(client, name, "exited")
156 BbsLibrary.print_status(client)
159 def print_status(client):
160 print("containers status")
161 for c in client.containers.list(all=True):
162 print(c.name, " ", c.status)
165 def wait_for_status(client, name, status):
166 while not BbsLibrary.is_in_status(client, name, status):
167 print ("waiting for container: ", name, "to be in status: ", status)
171 def is_in_status(client, name, status):
172 return len(client.containers.list(all=True, filters={"name": "^/"+name+"$", "status": status})) == 1