-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.py
382 lines (291 loc) · 12 KB
/
main.py
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
"""Main module of kytos/storehouse Kytos Network Application.
Persistence NApp with support for multiple backends.
"""
import json
import re
from datetime import datetime
from uuid import uuid4
from flask import jsonify, request
from kytos.core import KytosNApp, log, rest
from kytos.core.helpers import listen_to
from napps.kytos.storehouse import settings # pylint: disable=unused-import
def metadata_from_box(box):
"""Return a metadata from box."""
return {"box_id": box.box_id,
"owner": box.owner,
"created_at": box.created_at}
class Box:
"""Store data with the necessary metadata."""
def __init__(self, data, namespace, box_id=None):
"""Create a new Box instance.
Args:
data: Data to be stored in the box.
namespace: Namespace where the box belongs.
"""
self.data = data
self.namespace = namespace
if box_id is None:
box_id = uuid4().hex
self.box_id = box_id
self.created_at = str(datetime.utcnow())
self.owner = None
def __str__(self):
return '%s.%s' % (self.namespace, self.box_id)
@classmethod
def from_json(cls, json_data):
"""Create a new Box instance from JSON input."""
raw = json.loads(json_data)
data = raw.get('data')
namespace = raw.get('namespace')
return cls(data, namespace)
def to_dict(self):
"""Return the instance as a python dictionary."""
return {'data': self.data,
'namespace': self.namespace,
'owner': self.owner,
'created_at': self.created_at,
'id': self.box_id
}
def to_json(self):
"""Return the instance as a JSON string."""
return json.dumps(self.to_dict(), indent=4)
class Main(KytosNApp):
"""Main class of kytos/storehouse NApp.
This class is the entry point for this napp.
"""
metadata_cache = {}
def setup(self):
"""Replace the '__init__' method for the KytosNApp subclass.
Execute right after the NApp is loaded.
"""
# pylint: disable=import-outside-toplevel
if settings.BACKEND == "etcd":
from napps.kytos.storehouse.backends.etcd import Etcd
log.info("Loading 'etcd' backend...")
self.backend = Etcd()
else:
from napps.kytos.storehouse.backends.fs import FileSystem
log.info("Loading 'filesystem' backend...")
self.backend = FileSystem()
self.metadata_cache = {}
self.create_cache()
log.info("Storehouse NApp started.")
def execute(self):
"""Execute after the setup method."""
def create_cache(self):
"""Create a cache from all namespaces when the napp setup."""
log.debug('Creating storehouse cache...')
for namespace in self.backend.list_namespaces():
if namespace not in self.metadata_cache:
self.metadata_cache[namespace] = []
for box_id in self.backend.list(namespace):
box = self.backend.retrieve(namespace, box_id)
log.debug("Loading box '%s'...", box)
cache = metadata_from_box(box)
self.metadata_cache[namespace].append(cache)
def delete_metadata_from_cache(self, namespace, box_id=None):
"""Delete a metadata from cache.
Args:
namespace(str): A namespace, when the box will be deleted
box_id(str): Box identifier
"""
for cache in self.metadata_cache.get(namespace, []):
if (box_id and box_id in cache["box_id"]):
self.metadata_cache.get(namespace, []).remove(cache)
def add_metadata_to_cache(self, box):
"""Add a box cache into the namespace cache."""
cache = metadata_from_box(box)
if box.namespace not in self.metadata_cache:
self.metadata_cache[box.namespace] = []
self.metadata_cache[box.namespace].append(cache)
def search_metadata_by(self, namespace, filter_option="box_id", query=""):
"""Search for all metadata with specific pattern.
Args:
namespace(str): namespace where the box is stored
filter_option(str): metadata option
query(str): query to be searched
Returns:
list: list of metadata box filtered
"""
namespace_cache = self.metadata_cache.get(namespace, [])
results = []
for metadata in namespace_cache:
field_value = metadata.get(filter_option, "")
if re.match(f".*{query}.*", field_value):
results.append(metadata)
return results
@staticmethod
def _execute_callback(event, data, error):
"""Run the callback function for event calls to the NApp."""
try:
event.content['callback'](event, data, error)
except KeyError:
log.error(f'Event {event!r} without callback function!')
except TypeError as exception:
log.error(f"Bad callback function {event.content['callback']}!")
log.error(exception)
@rest('v1/<namespace>', methods=['POST'])
def rest_create(self, namespace):
"""Create a box in a namespace based on JSON input."""
data = request.get_json(silent=True)
if not data:
return jsonify({"response": "Invalid Request"}), 400
box = Box(data, namespace)
self.backend.create(box)
self.add_metadata_to_cache(box)
result = {"response": "Box created.", "id": box.box_id}
return jsonify(result), 201
@rest('v2/<namespace>', methods=['POST'])
@rest('v2/<namespace>/<box_id>', methods=['POST'])
def rest_create_v2(self, namespace, box_id=None):
"""Create a box in a namespace based on JSON input."""
data = request.get_json(silent=True)
if not data:
return jsonify({"response": "Invalid Request"}), 400
box = Box(data, namespace, box_id=box_id)
self.backend.create(box)
self.add_metadata_to_cache(box)
result = {"response": "Box created.", "id": box.box_id}
return jsonify(result), 201
@rest('v1/<namespace>', methods=['GET'])
def rest_list(self, namespace):
"""List all boxes in a namespace."""
result = self.backend.list(namespace)
return jsonify(result), 200
@rest('v1/<namespace>/<box_id>', methods=['PUT', 'PATCH'])
def rest_update(self, namespace, box_id):
"""Update a box_id from namespace."""
data = request.get_json(silent=True)
if not data:
return jsonify({"response": "Invalid request: empty data"}), 400
box = self.backend.retrieve(namespace, box_id)
if not box:
return jsonify({"response": "Not Found"}), 404
if request.method == "PUT":
box.data = data
else:
box.data.update(data)
self.backend.update(namespace, box)
return jsonify(box.data), 200
@rest('v1/<namespace>/<box_id>', methods=['GET'])
def rest_retrieve(self, namespace, box_id):
"""Retrieve and return a box from a namespace."""
box = self.backend.retrieve(namespace, box_id)
if not box:
return jsonify({"response": "Not Found"}), 404
return jsonify(box.data), 200
@rest('v1/<namespace>/<box_id>', methods=['DELETE'])
def rest_delete(self, namespace, box_id):
"""Delete a box from a namespace."""
result = self.backend.delete(namespace, box_id)
if result:
self.delete_metadata_from_cache(namespace, box_id)
return jsonify({"response": "Box deleted"}), 200
# or 204 - No Content
return jsonify({"response": "Box not found"}), 404
@rest("v1/<namespace>/search_by/<filter_option>/<query>", methods=['GET'])
def rest_search_by(self, namespace, filter_option="name", query=""):
"""Filter the boxes with specific pattern.
Args:
namespace(str): namespace where the box is stored
filter_option(str): metadata option
query(str): query to be searched
Returns:
list: list of metadata box filtered
"""
results = self.search_metadata_by(namespace, filter_option, query)
if not results:
return jsonify({"response": f"{filter_option} not found"}), 404
return jsonify(results), 200
@listen_to('kytos.storehouse.create')
def event_create(self, event):
"""Create a box in a namespace based on an event."""
error = None
box_id = event.content.get('box_id')
try:
data = event.content['data']
namespace = event.content['namespace']
if self.search_metadata_by(namespace, query=box_id):
raise KeyError("Box id already exists.")
except KeyError as exc:
box = None
error = exc
else:
box = Box(data, namespace, box_id=box_id)
self.backend.create(box)
self.add_metadata_to_cache(box)
self._execute_callback(event, box, error)
@listen_to('kytos.storehouse.retrieve')
def event_retrieve(self, event):
"""Retrieve a box from a namespace based on an event."""
error = None
try:
box = self.backend.retrieve(event.content['namespace'],
event.content['box_id'])
except KeyError as exc:
box = None
error = exc
self._execute_callback(event, box, error)
@listen_to('kytos.storehouse.update')
def event_update(self, event):
"""Update a box_id from namespace.
This method receive an KytosEvent with the content bellow.
namespace: namespace where the box is stored
box_id: the box identify
method: 'PUT' or 'PATCH', the default update method is 'PATCH'
data: a python dict with the data
"""
error = False
try:
namespace = event.content['namespace']
box_id = event.content['box_id']
box = self.backend.retrieve(namespace, box_id)
if not box:
raise KeyError("Box id does not exist.")
except KeyError as exc:
box = None
error = exc
else:
method = event.content.get('method', 'PATCH')
data = event.content.get('data', {})
if method == 'PUT':
box.data = data
elif method == 'PATCH':
box.data.update(data)
self.backend.update(namespace, box)
self._execute_callback(event, box, error)
@listen_to('kytos.storehouse.delete')
def event_delete(self, event):
"""Delete a box from a namespace based on an event."""
error = None
try:
namespace = event.content['namespace']
box_id = event.content['box_id']
except KeyError as exc:
result = None
error = exc
else:
result = self.backend.delete(namespace, box_id)
self.delete_metadata_from_cache(namespace, box_id)
self._execute_callback(event, result, error)
@listen_to('kytos.storehouse.list')
def event_list(self, event):
"""List all boxes in a namespace based on an event."""
error = None
try:
result = self.backend.list(event.content['namespace'])
except KeyError as exc:
result = None
error = exc
self._execute_callback(event, result, error)
@rest("v1/backup/<namespace>/", methods=['GET'])
@rest("v1/backup/<namespace>/<box_id>", methods=['GET'])
def rest_backup(self, namespace, box_id=None):
"""Backup an entire namespace or an object based on its id."""
try:
return jsonify(self.backend.backup(namespace, box_id)), 200
except ValueError:
return jsonify({"response": "Not Found"}), 404
def shutdown(self):
"""Execute before the NApp is unloaded."""
log.info("Storehouse NApp is shutting down.")