aboutsummaryrefslogtreecommitdiffstats
path: root/test/mocks/prov-mns-provider/src/ProvMnSProvider.py
blob: e9d488edef2c73e8cde45a7d77b4c7c2c533e36f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
from  http.server import HTTPServer, BaseHTTPRequestHandler
import re
import json
import base64
from urllib.parse import urlparse, parse_qs

with open("DefinedNRMFunction.json",'r') as f:
    jsonFile = json.loads(f.read())
SupportingFunctionList = jsonFile["NRMFunction"]

with open("UserInfo.json",'r') as f:
    UserFile = json.loads(f.read())

with open("ConfigInfo.json",'r') as f:
    ConfigFile = json.loads(f.read())

with open("preSetMOI.json",'r') as f:
    Cretaed_MOIs = json.loads(f.read())
Cretaed_MOIs_list = Cretaed_MOIs['preSetMOI']

ipAddress = ConfigFile["ipAddress"]
portNumber = ConfigFile["portNumber"]
prefix = ConfigFile["prefix"]

username = UserFile['userName']
password = UserFile['password']
Auth_str = username+":"+password
print(Auth_str)
base64string = base64.b64encode(bytes(Auth_str,'utf-8'))
authheader =  "Basic %s" % base64string.decode('utf-8')
print(authheader)

class ServerHTTP(BaseHTTPRequestHandler):
    def do_GET(self):
        path = self.path
        print("\n**************************** NEW GET REQUEST ********************************")
        request = urlparse(path)
        print("the PATH of the received GET request:" + request.path)
        pathlist = request.path.split('/')
        if "/" + pathlist[1] + "/"+ pathlist[2] == prefix:
            prefix_check = True
        else:
            prefix_check = False
        className = pathlist[3]
        idName = pathlist[4]
        response = {}
        query_params = parse_qs(request.query)
        if self.headers['Authorization'] == authheader and prefix_check is True:
            if className in SupportingFunctionList:
                try:
                    print("the value of the scope : "+ str(query_params['scope']))
                    print("the value of the filter : "+ str(query_params['filter']))
                    print("the value of the fields : "+ str(query_params['fields']))
                except:
                    print("the request body doesn't follow the standard format")
                    response['error'] = "the request body doesn't follow the standard format"
                    print("Fail to get MOI object: "+'/' +className+'/'+idName)
                    self.send_response(406)
                else:
                    find_moi = False
                    for MOI in Cretaed_MOIs_list:
                        if (idName == MOI['id'] and className == MOI['class']):
                            find_moi = True
                            try:
                                attributes = {}
                                for field in query_params['fields']:
                                    attributes[field] = MOI['attributes'][field]
                            except:
                                print("the createed MOI doesn't contain the required attribute")
                                response['error'] = "the createed MOI doesn't contain the required attribute"
                                print("Fail to get MOI object: "+'/' +className+'/'+idName)
                                self.send_response(406)
                            else:
                                print("Successfully get MOI object: "+ className+'_'+idName)
                                response = {"data":[{"href":"/"+className+"/"+idName,"class":className,"id":idName,"attributes":attributes}]}
                                self.send_response(200)
                    if (find_moi is False):
                        response['error'] = {"errorInfo":"MOI does not exist"}
                        print("Fail to get MOI object: "+'/' +className+'/'+idName)
                        self.send_response(406)
            else:
                response['error'] = {"errorInfo":"MOI class not support"}
                print("Fail to get MOI object: "+'/' +className+'/'+idName)
                self.send_response(406)
        else:
            self.send_response(401)
            if prefix_check is True:
                response['error'] = {"errorInfo":"not Authorized"}
            else:
                response['error'] = {"errorInfo":"wrong prefix"}
        self.send_header("Content-type","application/json")
        self.end_headers()
        buf = json.dumps(response)
        self.wfile.write(bytes(buf,'utf-8'))

    def do_PATCH(self):
        path = self.path
        print("\n**************************** NEW PATCH REQUEST ********************************")
        request = urlparse(path)
        print("the PATH of the received GET request:" + request.path)
        pathlist = request.path.split('/')
        if "/" + pathlist[1] + "/"+ pathlist[2] == prefix:
            prefix_check = True
        else:
            prefix_check = False
        className = pathlist[3]
        idName = pathlist[4]
        response = {}
        query_params = parse_qs(request.query)
        if self.headers['Authorization'] == authheader and prefix_check is True:
            if className in SupportingFunctionList:
                datas = self.rfile.read(int(self.headers['content-length']))
                json_str = datas.decode('utf-8')
                json_str = re.sub('\'','\"', json_str)
                json_dict = json.loads(json_str)
                try:
                    print("the value of the scope : "+ str(query_params['scope']))
                    print("the value of the filter : "+ str(query_params['filter']))
                    print("the modified attribute values : "+json.dumps(json_dict['data']))
                except:
                    print("the request body doesn't follow the standard format")
                    response['error'] = "the request body doesn't follow the standard format"
                    print("Fail to modify MOI object: "+'/' +className+'/'+idName)
                    self.send_response(406)
                else:
                    find_moi = False
                    for MOI in Cretaed_MOIs_list:
                        if (idName == MOI['id'] and className == MOI['class']):
                            find_moi = True
                            wrong_attribute = False
                            for key, value in json_dict['data'].items():
                                if (key in MOI['attributes']):
                                    MOI['attributes'][key] = value
                                else:
                                    wrong_attribute = True
                            if (wrong_attribute is True):
                                print("the createed MOI doesn't contain the required attribute")
                                response['error'] = "the createed MOI doesn't contain the required attribute"
                                print("Fail to get modify object: "+'/' +className+'/'+idName)
                                self.send_response(406)
                            else:
                                print("Successfully modify MOI object: "+ className+'_'+idName)
                                response = {"data":[MOI]}
                                self.send_response(200)
                    if (find_moi is False):
                        response['error'] = {"errorInfo":"MOI does not exist"}
                        print("Fail to get MOI object: "+'/' +className+'/'+idName)
                        self.send_response(406)
            else:
                response['error'] = {"errorInfo":"MOI class not support"}
                print("Fail to modify MOI object: "+'/' +className+'/'+idName)
                self.send_response(406)
        else:
            self.send_response(401)
            if prefix_check is True:
                response['error'] = {"errorInfo":"not Authorized"}
            else:
                response['error'] = {"errorInfo":"wrong prefix"}
        self.send_header("Content-type","application/json")
        self.end_headers()
        buf = json.dumps(response)
        self.wfile.write(bytes(buf,'utf-8'))

    def do_DELETE(self):
        path = self.path
        print("\n**************************** NEW DELETE REQUEST ********************************")
        request = urlparse(path)
        print("the PATH of the received DELETE request:" + request.path)
        pathlist = request.path.split('/')
        if "/" + pathlist[1] + "/"+ pathlist[2] == prefix:
            prefix_check = True
        else:
            prefix_check = False
        className = pathlist[3]
        idName = pathlist[4]
        response = {}
        query_params = parse_qs(request.query)
        if self.headers['Authorization'] == authheader and prefix_check is True:
            if className in SupportingFunctionList:
                try:
                    print("the value of the scope : "+ str(query_params['scope']))
                    print("the value of the filter : "+ str(query_params['filter']))
                except:
                    print("the request body doesn't follow the standard format")
                    response['error'] = "the request body doesn't follow the standard format"
                    print("Fail to delete MOI object: "+'/' +className+'/'+idName)
                    self.send_response(406)
                else:
                    find_moi = False
                    for MOI in Cretaed_MOIs_list:
                        if (idName == MOI['id'] and className == MOI['class']):
                            find_moi = True
                            Cretaed_MOIs_list.remove(MOI)
                            print("Successfully delete MOI object: "+ className+'_'+idName)
                            response = {"data":["/"+className+"/"+idName]}
                            self.send_response(200)
                    if (find_moi is False):
                        response['error'] = {"errorInfo":"MOI does not exist"}
                        print("Fail to delete MOI object: "+'/' +className+'/'+idName)
                        self.send_response(406)
            else:
                response['error'] = {"errorInfo":"MOI class not support"}
                print("Fail to delete MOI object: "+'/' +className+'/'+idName)
                self.send_response(406)
        else:
            self.send_response(401)
            if prefix_check is True:
                response['error'] = {"errorInfo":"not Authorized"}
            else:
                response['error'] = {"errorInfo":"wrong prefix"}
        self.send_header("Content-type","application/json")
        self.end_headers()
        buf = json.dumps(response)
        self.wfile.write(bytes(buf,'utf-8'))

    def do_PUT(self):
        path = self.path
        print("\n**************************** NEW PUT REQUEST ********************************")
        print("the PATH of the received PUT request:" + path)
        pathlist = path.split('/')
        if "/" + pathlist[1] + "/"+ pathlist[2] == prefix:
            prefix_check = True
        else:
            prefix_check = False
        className = pathlist[3]
        idName = pathlist[4]
        response = {}
        if self.headers['Authorization'] == authheader and prefix_check is True:
            if className in SupportingFunctionList:
                datas = self.rfile.read(int(self.headers['content-length']))
                json_str = datas.decode('utf-8')
                json_str = re.sub('\'','\"', json_str)
                json_dict = json.loads(json_str)
                try:
                    print("the class of the New MOI : "+json_dict['data']['class'])
                    print("the ID of the New MOI : "+json_dict['data']['id'])
                    print("the href of the New MOI : "+json_dict['data']['href'])
                    print("the attributes of the New MOI : "+json.dumps(json_dict['data']['attributes']))
                except:
                    print("the request body doesn't follow the standard format")
                    response['error'] = "the request body doesn't follow the standard format"
                    print("Fail to create MOI object: "+'/' +className+'/'+idName)
                    self.send_response(406)
                else:
                    print("Successfully create MOI object: "+ className+'/'+idName)
                    Cretaed_MOIs_list.append(json_dict['data'])
                    response = json_dict
                    self.send_response(201)
                    self.send_header("Location",path)
            else:
                response['error'] = {"errorInfo":"MOI class not support"}
                print("Fail to create MOI object: "+'/' +className+'/'+idName)
                self.send_response(406)
        else:
            self.send_response(401)
            if prefix_check is True:
                response['error'] = {"errorInfo":"not Authorized"}
            else:
                response['error'] = {"errorInfo":"wrong prefix"}
        self.send_header("Content-type","application/json")
        self.end_headers()
        buf = json.dumps(response)
        self.wfile.write(bytes(buf,'utf-8'))

def start_server(port):
    http_server = HTTPServer((ipAddress, int(port)), ServerHTTP)
    http_server.serve_forever()

if __name__ == "__main__":
    start_server(int(portNumber))