Huang Cheng | ba7629d | 2020-03-03 09:01:58 +0000 | [diff] [blame] | 1 | from http.server import HTTPServer, BaseHTTPRequestHandler |
| 2 | import re |
| 3 | import json |
| 4 | import base64 |
| 5 | from urllib.parse import urlparse, parse_qs |
| 6 | |
| 7 | with open("DefinedNRMFunction.json",'r') as f: |
| 8 | jsonFile = json.loads(f.read()) |
| 9 | SupportingFunctionList = jsonFile["NRMFunction"] |
| 10 | |
| 11 | with open("UserInfo.json",'r') as f: |
| 12 | UserFile = json.loads(f.read()) |
| 13 | |
| 14 | with open("ConfigInfo.json",'r') as f: |
| 15 | ConfigFile = json.loads(f.read()) |
| 16 | |
| 17 | with open("preSetMOI.json",'r') as f: |
| 18 | Cretaed_MOIs = json.loads(f.read()) |
| 19 | Cretaed_MOIs_list = Cretaed_MOIs['preSetMOI'] |
| 20 | |
| 21 | ipAddress = ConfigFile["ipAddress"] |
| 22 | portNumber = ConfigFile["portNumber"] |
| 23 | prefix = ConfigFile["prefix"] |
| 24 | |
| 25 | username = UserFile['userName'] |
| 26 | password = UserFile['password'] |
| 27 | Auth_str = username+":"+password |
| 28 | print(Auth_str) |
| 29 | base64string = base64.b64encode(bytes(Auth_str,'utf-8')) |
| 30 | authheader = "Basic %s" % base64string.decode('utf-8') |
| 31 | print(authheader) |
| 32 | |
| 33 | class ServerHTTP(BaseHTTPRequestHandler): |
| 34 | def do_GET(self): |
| 35 | path = self.path |
| 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('/') |
| 40 | if "/" + pathlist[1] + "/"+ pathlist[2] == prefix: |
| 41 | prefix_check = True |
| 42 | else: |
| 43 | prefix_check = False |
| 44 | className = pathlist[3] |
| 45 | idName = pathlist[4] |
| 46 | response = {} |
| 47 | query_params = parse_qs(request.query) |
| 48 | if self.headers['Authorization'] == authheader and prefix_check is True: |
| 49 | if className in SupportingFunctionList: |
| 50 | try: |
| 51 | print("the value of the scope : "+ str(query_params['scope'])) |
| 52 | print("the value of the filter : "+ str(query_params['filter'])) |
| 53 | print("the value of the fields : "+ str(query_params['fields'])) |
| 54 | except: |
| 55 | print("the request body doesn't follow the standard format") |
| 56 | response['error'] = "the request body doesn't follow the standard format" |
| 57 | print("Fail to get MOI object: "+'/' +className+'/'+idName) |
| 58 | self.send_response(406) |
| 59 | else: |
| 60 | find_moi = False |
| 61 | for MOI in Cretaed_MOIs_list: |
| 62 | if (idName == MOI['id'] and className == MOI['class']): |
| 63 | find_moi = True |
| 64 | try: |
| 65 | attributes = {} |
| 66 | for field in query_params['fields']: |
| 67 | attributes[field] = MOI['attributes'][field] |
| 68 | except: |
| 69 | print("the createed MOI doesn't contain the required attribute") |
| 70 | response['error'] = "the createed MOI doesn't contain the required attribute" |
| 71 | print("Fail to get MOI object: "+'/' +className+'/'+idName) |
| 72 | self.send_response(406) |
| 73 | else: |
| 74 | print("Successfully get MOI object: "+ className+'_'+idName) |
| 75 | response = {"data":[{"href":"/"+className+"/"+idName,"class":className,"id":idName,"attributes":attributes}]} |
| 76 | self.send_response(200) |
| 77 | if (find_moi is False): |
| 78 | response['error'] = {"errorInfo":"MOI does not exist"} |
| 79 | print("Fail to get MOI object: "+'/' +className+'/'+idName) |
| 80 | self.send_response(406) |
| 81 | else: |
| 82 | response['error'] = {"errorInfo":"MOI class not support"} |
| 83 | print("Fail to get MOI object: "+'/' +className+'/'+idName) |
| 84 | self.send_response(406) |
| 85 | else: |
| 86 | self.send_response(401) |
| 87 | if prefix_check is True: |
| 88 | response['error'] = {"errorInfo":"not Authorized"} |
| 89 | else: |
| 90 | response['error'] = {"errorInfo":"wrong prefix"} |
| 91 | self.send_header("Content-type","application/json") |
| 92 | self.end_headers() |
| 93 | buf = json.dumps(response) |
| 94 | self.wfile.write(bytes(buf,'utf-8')) |
| 95 | |
| 96 | def do_PATCH(self): |
| 97 | path = self.path |
| 98 | print("\n**************************** NEW PATCH REQUEST ********************************") |
| 99 | request = urlparse(path) |
| 100 | print("the PATH of the received GET request:" + request.path) |
| 101 | pathlist = request.path.split('/') |
| 102 | if "/" + pathlist[1] + "/"+ pathlist[2] == prefix: |
| 103 | prefix_check = True |
| 104 | else: |
| 105 | prefix_check = False |
| 106 | className = pathlist[3] |
| 107 | idName = pathlist[4] |
| 108 | response = {} |
| 109 | query_params = parse_qs(request.query) |
| 110 | if self.headers['Authorization'] == authheader and prefix_check is True: |
| 111 | if className in SupportingFunctionList: |
| 112 | datas = self.rfile.read(int(self.headers['content-length'])) |
| 113 | json_str = datas.decode('utf-8') |
| 114 | json_str = re.sub('\'','\"', json_str) |
| 115 | json_dict = json.loads(json_str) |
| 116 | try: |
| 117 | print("the value of the scope : "+ str(query_params['scope'])) |
| 118 | print("the value of the filter : "+ str(query_params['filter'])) |
| 119 | print("the modified attribute values : "+json.dumps(json_dict['data'])) |
| 120 | except: |
| 121 | print("the request body doesn't follow the standard format") |
| 122 | response['error'] = "the request body doesn't follow the standard format" |
| 123 | print("Fail to modify MOI object: "+'/' +className+'/'+idName) |
| 124 | self.send_response(406) |
| 125 | else: |
| 126 | find_moi = False |
| 127 | for MOI in Cretaed_MOIs_list: |
| 128 | if (idName == MOI['id'] and className == MOI['class']): |
| 129 | find_moi = True |
| 130 | wrong_attribute = False |
| 131 | for key, value in json_dict['data'].items(): |
| 132 | if (key in MOI['attributes']): |
| 133 | MOI['attributes'][key] = value |
| 134 | else: |
| 135 | wrong_attribute = True |
| 136 | if (wrong_attribute is True): |
| 137 | print("the createed MOI doesn't contain the required attribute") |
| 138 | response['error'] = "the createed MOI doesn't contain the required attribute" |
| 139 | print("Fail to get modify object: "+'/' +className+'/'+idName) |
| 140 | self.send_response(406) |
| 141 | else: |
| 142 | print("Successfully modify MOI object: "+ className+'_'+idName) |
| 143 | response = {"data":[MOI]} |
| 144 | self.send_response(200) |
| 145 | if (find_moi is False): |
| 146 | response['error'] = {"errorInfo":"MOI does not exist"} |
| 147 | print("Fail to get MOI object: "+'/' +className+'/'+idName) |
| 148 | self.send_response(406) |
| 149 | else: |
| 150 | response['error'] = {"errorInfo":"MOI class not support"} |
| 151 | print("Fail to modify MOI object: "+'/' +className+'/'+idName) |
| 152 | self.send_response(406) |
| 153 | else: |
| 154 | self.send_response(401) |
| 155 | if prefix_check is True: |
| 156 | response['error'] = {"errorInfo":"not Authorized"} |
| 157 | else: |
| 158 | response['error'] = {"errorInfo":"wrong prefix"} |
| 159 | self.send_header("Content-type","application/json") |
| 160 | self.end_headers() |
| 161 | buf = json.dumps(response) |
| 162 | self.wfile.write(bytes(buf,'utf-8')) |
| 163 | |
| 164 | def do_DELETE(self): |
| 165 | path = self.path |
| 166 | print("\n**************************** NEW DELETE REQUEST ********************************") |
| 167 | request = urlparse(path) |
| 168 | print("the PATH of the received DELETE request:" + request.path) |
| 169 | pathlist = request.path.split('/') |
| 170 | if "/" + pathlist[1] + "/"+ pathlist[2] == prefix: |
| 171 | prefix_check = True |
| 172 | else: |
| 173 | prefix_check = False |
| 174 | className = pathlist[3] |
| 175 | idName = pathlist[4] |
| 176 | response = {} |
| 177 | query_params = parse_qs(request.query) |
| 178 | if self.headers['Authorization'] == authheader and prefix_check is True: |
| 179 | if className in SupportingFunctionList: |
| 180 | try: |
| 181 | print("the value of the scope : "+ str(query_params['scope'])) |
| 182 | print("the value of the filter : "+ str(query_params['filter'])) |
| 183 | except: |
| 184 | print("the request body doesn't follow the standard format") |
| 185 | response['error'] = "the request body doesn't follow the standard format" |
| 186 | print("Fail to delete MOI object: "+'/' +className+'/'+idName) |
| 187 | self.send_response(406) |
| 188 | else: |
| 189 | find_moi = False |
| 190 | for MOI in Cretaed_MOIs_list: |
| 191 | if (idName == MOI['id'] and className == MOI['class']): |
| 192 | find_moi = True |
| 193 | Cretaed_MOIs_list.remove(MOI) |
| 194 | print("Successfully delete MOI object: "+ className+'_'+idName) |
| 195 | response = {"data":["/"+className+"/"+idName]} |
| 196 | self.send_response(200) |
| 197 | if (find_moi is False): |
| 198 | response['error'] = {"errorInfo":"MOI does not exist"} |
| 199 | print("Fail to delete MOI object: "+'/' +className+'/'+idName) |
| 200 | self.send_response(406) |
| 201 | else: |
| 202 | response['error'] = {"errorInfo":"MOI class not support"} |
| 203 | print("Fail to delete MOI object: "+'/' +className+'/'+idName) |
| 204 | self.send_response(406) |
| 205 | else: |
| 206 | self.send_response(401) |
| 207 | if prefix_check is True: |
| 208 | response['error'] = {"errorInfo":"not Authorized"} |
| 209 | else: |
| 210 | response['error'] = {"errorInfo":"wrong prefix"} |
| 211 | self.send_header("Content-type","application/json") |
| 212 | self.end_headers() |
| 213 | buf = json.dumps(response) |
| 214 | self.wfile.write(bytes(buf,'utf-8')) |
| 215 | |
| 216 | def do_PUT(self): |
| 217 | path = self.path |
| 218 | print("\n**************************** NEW PUT REQUEST ********************************") |
| 219 | print("the PATH of the received PUT request:" + path) |
| 220 | pathlist = path.split('/') |
| 221 | if "/" + pathlist[1] + "/"+ pathlist[2] == prefix: |
| 222 | prefix_check = True |
| 223 | else: |
| 224 | prefix_check = False |
| 225 | className = pathlist[3] |
| 226 | idName = pathlist[4] |
| 227 | response = {} |
| 228 | if self.headers['Authorization'] == authheader and prefix_check is True: |
| 229 | if className in SupportingFunctionList: |
| 230 | datas = self.rfile.read(int(self.headers['content-length'])) |
| 231 | json_str = datas.decode('utf-8') |
| 232 | json_str = re.sub('\'','\"', json_str) |
| 233 | json_dict = json.loads(json_str) |
| 234 | try: |
| 235 | print("the class of the New MOI : "+json_dict['data']['class']) |
| 236 | print("the ID of the New MOI : "+json_dict['data']['id']) |
| 237 | print("the href of the New MOI : "+json_dict['data']['href']) |
| 238 | print("the attributes of the New MOI : "+json.dumps(json_dict['data']['attributes'])) |
| 239 | except: |
| 240 | print("the request body doesn't follow the standard format") |
| 241 | response['error'] = "the request body doesn't follow the standard format" |
| 242 | print("Fail to create MOI object: "+'/' +className+'/'+idName) |
| 243 | self.send_response(406) |
| 244 | else: |
| 245 | print("Successfully create MOI object: "+ className+'/'+idName) |
| 246 | Cretaed_MOIs_list.append(json_dict['data']) |
| 247 | response = json_dict |
| 248 | self.send_response(201) |
| 249 | self.send_header("Location",path) |
| 250 | else: |
| 251 | response['error'] = {"errorInfo":"MOI class not support"} |
| 252 | print("Fail to create MOI object: "+'/' +className+'/'+idName) |
| 253 | self.send_response(406) |
| 254 | else: |
| 255 | self.send_response(401) |
| 256 | if prefix_check is True: |
| 257 | response['error'] = {"errorInfo":"not Authorized"} |
| 258 | else: |
| 259 | response['error'] = {"errorInfo":"wrong prefix"} |
| 260 | self.send_header("Content-type","application/json") |
| 261 | self.end_headers() |
| 262 | buf = json.dumps(response) |
| 263 | self.wfile.write(bytes(buf,'utf-8')) |
| 264 | |
| 265 | def start_server(port): |
| 266 | http_server = HTTPServer((ipAddress, int(port)), ServerHTTP) |
| 267 | http_server.serve_forever() |
| 268 | |
| 269 | if __name__ == "__main__": |
| 270 | start_server(int(portNumber)) |