1 from http.server import HTTPServer, BaseHTTPRequestHandler
5 from urllib.parse import urlparse, parse_qs
7 with open("DefinedNRMFunction.json",'r') as f:
8 jsonFile = json.loads(f.read())
9 SupportingFunctionList = jsonFile["NRMFunction"]
11 with open("UserInfo.json",'r') as f:
12 UserFile = json.loads(f.read())
14 with open("ConfigInfo.json",'r') as f:
15 ConfigFile = json.loads(f.read())
17 with open("preSetMOI.json",'r') as f:
18 Cretaed_MOIs = json.loads(f.read())
19 Cretaed_MOIs_list = Cretaed_MOIs['preSetMOI']
21 ipAddress = ConfigFile["ipAddress"]
22 portNumber = ConfigFile["portNumber"]
23 prefix = ConfigFile["prefix"]
25 username = UserFile['userName']
26 password = UserFile['password']
27 Auth_str = username+":"+password
29 base64string = base64.b64encode(bytes(Auth_str,'utf-8'))
30 authheader = "Basic %s" % base64string.decode('utf-8')
33 class ServerHTTP(BaseHTTPRequestHandler):
36 print("\n**************************** NEW GET REQUEST ********************************")
37 request = urlparse(path)
38 print("the PATH of the received GET request:" + request.path)
39 pathlist = request.path.split('/')
42 if "/" + pathlist[1] + "/"+ pathlist[2] != prefix:
44 className = pathlist[3]
49 query_params = parse_qs(request.query)
50 if self.headers['Authorization'] == authheader and prefix_check is True:
51 if className in SupportingFunctionList:
53 print("the value of the scope : "+ str(query_params['scope']))
54 print("the value of the filter : "+ str(query_params['filter']))
55 print("the value of the fields : "+ str(query_params['fields']))
57 print("the request body doesn't follow the standard format")
58 response['error'] = "the request body doesn't follow the standard format"
59 print("Fail to get MOI object: "+'/' +className+'/'+idName)
60 self.send_response(406)
63 for MOI in Cretaed_MOIs_list:
64 if (idName == MOI['id'] and className == MOI['class']):
68 for field in query_params['fields']:
69 attributes[field] = MOI['attributes'][field]
71 print("the createed MOI doesn't contain the required attribute")
72 response['error'] = "the createed MOI doesn't contain the required attribute"
73 print("Fail to get MOI object: "+'/' +className+'/'+idName)
74 self.send_response(406)
76 print("Successfully get MOI object: "+ className+'_'+idName)
77 response = {"data":[{"href":"/"+className+"/"+idName,"class":className,"id":idName,"attributes":attributes}]}
78 self.send_response(200)
79 if (find_moi is False):
80 response['error'] = {"errorInfo":"MOI does not exist"}
81 print("Fail to get MOI object: "+'/' +className+'/'+idName)
82 self.send_response(406)
84 response['error'] = {"errorInfo":"MOI class not support"}
85 print("Fail to get MOI object: "+'/' +className+'/'+idName)
86 self.send_response(406)
88 if prefix_check is True:
89 self.send_response(401)
90 response['error'] = {"errorInfo":"not Authorized"}
92 self.send_response(404)
93 response['error'] = {"errorInfo":"wrong prefix"}
94 self.send_header("Content-type","application/json")
96 buf = json.dumps(response)
97 self.wfile.write(bytes(buf,'utf-8'))
101 print("\n**************************** NEW PATCH REQUEST ********************************")
102 request = urlparse(path)
103 print("the PATH of the received GET request:" + request.path)
104 pathlist = request.path.split('/')
107 if "/" + pathlist[1] + "/"+ pathlist[2] != prefix:
109 className = pathlist[3]
114 query_params = parse_qs(request.query)
115 if self.headers['Authorization'] == authheader and prefix_check is True:
116 if className in SupportingFunctionList:
117 datas = self.rfile.read(int(self.headers['content-length']))
118 json_str = datas.decode('utf-8')
119 json_str = re.sub('\'','\"', json_str)
120 json_dict = json.loads(json_str)
122 print("the value of the scope : "+ str(query_params['scope']))
123 print("the value of the filter : "+ str(query_params['filter']))
124 print("the modified attribute values : "+json.dumps(json_dict['data']))
126 print("the request body doesn't follow the standard format")
127 response['error'] = "the request body doesn't follow the standard format"
128 print("Fail to modify MOI object: "+'/' +className+'/'+idName)
129 self.send_response(406)
132 for MOI in Cretaed_MOIs_list:
133 if (idName == MOI['id'] and className == MOI['class']):
135 wrong_attribute = False
136 for key, value in json_dict['data'].items():
137 if (key in MOI['attributes']):
138 MOI['attributes'][key] = value
140 wrong_attribute = True
141 if (wrong_attribute is True):
142 print("the createed MOI doesn't contain the required attribute")
143 response['error'] = "the createed MOI doesn't contain the required attribute"
144 print("Fail to get modify object: "+'/' +className+'/'+idName)
145 self.send_response(406)
147 print("Successfully modify MOI object: "+ className+'_'+idName)
148 response = {"data":[MOI]}
149 self.send_response(200)
150 if (find_moi is False):
151 response['error'] = {"errorInfo":"MOI does not exist"}
152 print("Fail to get MOI object: "+'/' +className+'/'+idName)
153 self.send_response(406)
155 response['error'] = {"errorInfo":"MOI class not support"}
156 print("Fail to modify MOI object: "+'/' +className+'/'+idName)
157 self.send_response(406)
159 if prefix_check is True:
160 self.send_response(401)
161 response['error'] = {"errorInfo":"not Authorized"}
163 self.send_response(404)
164 response['error'] = {"errorInfo":"wrong prefix"}
165 self.send_header("Content-type","application/json")
167 buf = json.dumps(response)
168 self.wfile.write(bytes(buf,'utf-8'))
172 print("\n**************************** NEW DELETE REQUEST ********************************")
173 request = urlparse(path)
174 print("the PATH of the received DELETE request:" + request.path)
175 pathlist = request.path.split('/')
178 if "/" + pathlist[1] + "/"+ pathlist[2] != prefix:
180 className = pathlist[3]
185 query_params = parse_qs(request.query)
186 if self.headers['Authorization'] == authheader and prefix_check is True:
187 if className in SupportingFunctionList:
189 print("the value of the scope : "+ str(query_params['scope']))
190 print("the value of the filter : "+ str(query_params['filter']))
192 print("the request body doesn't follow the standard format")
193 response['error'] = "the request body doesn't follow the standard format"
194 print("Fail to delete MOI object: "+'/' +className+'/'+idName)
195 self.send_response(406)
198 for MOI in Cretaed_MOIs_list:
199 if (idName == MOI['id'] and className == MOI['class']):
201 Cretaed_MOIs_list.remove(MOI)
202 print("Successfully delete MOI object: "+ className+'_'+idName)
203 response = {"data":["/"+className+"/"+idName]}
204 self.send_response(200)
205 if (find_moi is False):
206 response['error'] = {"errorInfo":"MOI does not exist"}
207 print("Fail to delete MOI object: "+'/' +className+'/'+idName)
208 self.send_response(406)
210 response['error'] = {"errorInfo":"MOI class not support"}
211 print("Fail to delete MOI object: "+'/' +className+'/'+idName)
212 self.send_response(406)
214 if prefix_check is True:
215 self.send_response(401)
216 response['error'] = {"errorInfo":"not Authorized"}
218 self.send_response(404)
219 response['error'] = {"errorInfo":"wrong prefix"}
220 self.send_header("Content-type","application/json")
222 buf = json.dumps(response)
223 self.wfile.write(bytes(buf,'utf-8'))
227 print("\n**************************** NEW PUT REQUEST ********************************")
228 print("the PATH of the received PUT request:" + path)
229 pathlist = path.split('/')
232 if "/" + pathlist[1] + "/"+ pathlist[2] != prefix:
234 className = pathlist[3]
239 if self.headers['Authorization'] == authheader and prefix_check is True:
240 if className in SupportingFunctionList:
241 datas = self.rfile.read(int(self.headers['content-length']))
242 json_str = datas.decode('utf-8')
243 json_str = re.sub('\'','\"', json_str)
244 json_dict = json.loads(json_str)
246 print("the class of the New MOI : "+json_dict['data']['class'])
247 print("the ID of the New MOI : "+json_dict['data']['id'])
248 print("the href of the New MOI : "+json_dict['data']['href'])
249 print("the attributes of the New MOI : "+json.dumps(json_dict['data']['attributes']))
251 print("the request body doesn't follow the standard format")
252 response['error'] = "the request body doesn't follow the standard format"
253 print("Fail to create MOI object: "+'/' +className+'/'+idName)
254 self.send_response(406)
256 print("Successfully create MOI object: "+ className+'/'+idName)
257 Cretaed_MOIs_list.append(json_dict['data'])
259 self.send_response(201)
260 self.send_header("Location",path)
262 response['error'] = {"errorInfo":"MOI class not support"}
263 print("Fail to create MOI object: "+'/' +className+'/'+idName)
264 self.send_response(406)
266 if prefix_check is True:
267 self.send_response(401)
268 response['error'] = {"errorInfo":"not Authorized"}
270 self.send_response(404)
271 response['error'] = {"errorInfo":"wrong prefix"}
272 self.send_header("Content-type","application/json")
274 buf = json.dumps(response)
275 self.wfile.write(bytes(buf,'utf-8'))
277 def start_server(port):
278 http_server = HTTPServer((ipAddress, int(port)), ServerHTTP)
279 http_server.serve_forever()
281 if __name__ == "__main__":
282 start_server(int(portNumber))