-
Notifications
You must be signed in to change notification settings - Fork 16
/
AnnouncementManager.py
567 lines (438 loc) · 20.1 KB
/
AnnouncementManager.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
#
# AnnouncementManager.py
#
# (c) 2020 by Andreas Kraft
# License: BSD 3-Clause License. See the LICENSE file for further details.
#
# Managing entity for resource announcements
#
""" This module defines the manager for handling announcements.
"""
from __future__ import annotations
from typing import Optional, Tuple, List, cast, Any
import time
from ..etc.ACMEUtils import isSPRelative
from ..helpers.TextTools import findXPath
from ..etc.Types import DesiredIdentifierResultType, ResourceTypes, JSON, ResultContentType, CSERequest, FilterCriteria
from ..etc.Types import Operation
from ..etc.ResponseStatusCodes import ResponseStatusCode, ResponseException
from ..etc.ResponseStatusCodes import BAD_REQUEST, INTERNAL_SERVER_ERROR, OPERATION_NOT_ALLOWED, CONFLICT
from ..etc.Constants import Constants
from ..resources.Resource import Resource
from ..resources.AnnounceableResource import AnnounceableResource
from ..resources.CSEBase import getCSE
from ..runtime import CSE
from ..runtime.Configuration import Configuration
from ..runtime.Logging import Logging as L
# TODO for anounceable resource:
# - update: update resource here
class AnnouncementManager(object):
""" This class implements announcement functionalities.
Attributes:
checkInterval: Number of seconds to wait between tries to announce resources to remote CSEs (configurable).
allowAnnouncementsToHostingCSE: Allow or disallow resources to announce to the own hosting CSE (configurable).
delayAfterRegistration: Number of seconds to wait before performing announcements when a new CSE has registered (configurable).
"""
__slots__ = (
'checkInterval',
'allowAnnouncementsToHostingCSE',
'delayAfterRegistration',
)
def __init__(self) -> None:
""" Initialization of the announcement manager.
"""
CSE.event.addHandler(CSE.event.registeredToRegistrarCSE, self.handleRegisteredToRegistrarCSE) # type: ignore
CSE.event.addHandler(CSE.event.registreeCSEHasRegistered, self.handleRegistreeCSEHasRegistered) # type: ignore
# Configuration values
self._assignConfig()
# Add a handler for configuration changes
CSE.event.addHandler(CSE.event.configUpdate, self.configUpdate) # type: ignore
L.isInfo and L.log('AnnouncementManager initialized')
def shutdown(self) -> bool:
""" Shutdown the announcement manager.
Return:
Always True.
"""
if CSE.remote:
for csr in CSE.remote.getAllLocalCSRs():
if csr:
self.checkResourcesForUnAnnouncement(csr)
L.isInfo and L.log('AnnouncementManager shut down')
return True
def _assignConfig(self) -> None:
""" Store relevant configuration values in the announcement manager.
"""
self.checkInterval = Configuration.get('cse.announcements.checkInterval')
self.allowAnnouncementsToHostingCSE = Configuration.get('cse.announcements.allowAnnouncementsToHostingCSE')
self.delayAfterRegistration = Configuration.get('cse.announcements.delayAfterRegistration')
def configUpdate(self, name:str, key:Optional[str] = None, value:Optional[Any] = None) -> None:
""" Callback for the *configUpdate* event.
Args:
name: Event name.
key: Name of the updated configuration setting.
value: New value for the config setting.
"""
if key not in [ 'cse.announcements.checkInterval',
'cse.announcements.allowAnnouncementsToHostingCSE',
'cse.announcements.delayAfterRegistration',
]:
return
# assign new values
self._assignConfig()
#########################################################################
#
# Event Handlers. Listen on remote CSE registrations
#
def handleRegisteredToRegistrarCSE(self, name:str, remoteCSE:Resource, remoteCSR:Resource) -> None:
""" Handle registrations to a registrar CSE.
Args:
name:Event name.
remoteCSE: The remote `CSEBase` resource.
remoteCSR: The own CSE's remote `CSR` resource.
"""
time.sleep(self.delayAfterRegistration) # Give some time until remote CSE fully connected
self.checkResourcesForAnnouncement(remoteCSR)
def handleRegistreeCSEHasRegistered(self, name:str, remoteCSR:Resource) -> None:
""" Handle registrations when a registree CSE has registered.
Args:
name:Event name.
remoteCSR: The own CSE's remote `CSR` resource.
"""
time.sleep(self.delayAfterRegistration) # Give some time until remote CSE is fully connected
self.checkResourcesForAnnouncement(remoteCSR)
#########################################################################
#
# Access Methods to check, announce, unanounce etc
#
#
# Announcements
#
def checkResourcesForAnnouncement(self, remoteCSR:Resource) -> None:
""" Check all resources in the resource tree and announce them if necessary.
Args:
remoteCSR: The registree or registrar CSE's `CSR` resource.
See Also:
- `announceResource`
- `searchAnnounceableResourcesForCSI`
"""
if not remoteCSR:
return
# get all reources for this specific CSI that are NOT announced to it yet
resources = self.searchAnnounceableResourcesForCSI(remoteCSR.csi, False) # only return the resources that are *not* announced to this csi yet
# try to announce all not-announced resources to this csr
for resource in resources:
self.announceResource(resource)
def announceResource(self, resource:AnnounceableResource) -> None:
""" Announce a single resource to its announcement target(s).
Args:
resource: The resource to announce.
Return:
Result object indicating the success of the operation.
See Also:
- `announceResourceToCSI`
"""
L.isDebug and L.logDebug(f'Announce resource: {resource.ri} to all connected csr')
for at in resource.at:
if (at == CSE.cseCsi or at.startswith(CSE.cseCsiSlash)) and not self.allowAnnouncementsToHostingCSE:
L.isWarn and L.logWarn('Targeting own CSE for announcement. Ignored.')
self._removeAnnouncementFromResource(resource, at)
continue
self.announceResourceToCSI(resource, at) # ignore result
def announceResourceToCSI(self, resource:AnnounceableResource, csi:str) -> None:
""" Announce a resource to a specific registered remote CSE.
Args:
resource: The resource to announce.
csi: CSE-ID of the remote CSE.
Return:
Result object indicating the success of the operation.
TODO:
- Support announcement to direct URL
"""
# TODO direct URL
def checkCSEBaseAnnouncement(cseBase:AnnounceableResource) -> None:
""" Check and perform the announcement of a CSE.
Args:
cseBase: The announceable version of a `CSEBase` (`CSEBaseAnnc`).
Return:
Result object indicating the success of the operation.
"""
L.isDebug and L.logDebug(f'Check CSEBase announcement')
if t := self._announcedInfos(cseBase, csi):
L.isDebug and L.logDebug(f'announcement infos: {t}')
# CSEBase has "old" announcement infos
remoteRi = t[1] if isSPRelative(t[1]) else f'{csi}/{t[1]}'
try:
_r = CSE.dispatcher.retrieveResource(remoteRi, CSE.cseCsi)
except ResponseException as e: # basically anything that isn't "OK"
L.isDebug and L.logDebug('CSEBase is not announced')
# No, it's not there anymore -> announce it again.
self._removeAnnouncementFromResource(cseBase, csi)
# announce CSE recursively
self.announceResourceToCSI(cseBase, csi)
else:
# No internal announcement infos, try to discover it on the remote CSE.
# This is done by discovering a CSEBaseAnnc resource with a link to our CSE.
# Get the remote CSE's resource ID
# We don't know the name of the remote CSEBase, so we have to use the CSI + '-'
to = f'{csi}/-'
# Here, it is actually important NOT to get the next CSE, but to check whether
# there is a remots CSEBase with that ID. Only THEN we can send the request and
# continue with the announcement.
res = CSE.request.handleSendRequest(CSERequest(op = Operation.RETRIEVE,
to = to,
originator = CSE.cseCsi,
rcn = ResultContentType.childResourceReferences,
drt = DesiredIdentifierResultType.unstructured,
fc = FilterCriteria(ty = [ ResourceTypes.CSEBaseAnnc.value ],
attributes = { 'lnk' : f'{cseBase.csi}/{cseBase.ri}' } ))
)[0].result # there should be at least one result
if res.rsc == ResponseStatusCode.OK and res.data: # Found a remote CSEBaseAnnc
# Assign to the local CSEBase
if (remoteRi := findXPath(cast(dict, res.data), 'm2m:rrl/rrf/{0}/val')):
atri = remoteRi if isSPRelative(remoteRi) else f'{csi}/{remoteRi}'
L.isDebug and L.logDebug(f'CSEBase already announced: {atri}. Updating CSEBase announcement')
cseBase.addAnnouncementToResource(csi, remoteRi)
cseBase.dbUpdate()
return
# Not found, so announce it
L.isDebug and L.logDebug(f'announcing CSEBase: {cseBase.ri}')
self.announceResourceToCSI(cseBase, csi)
L.isDebug and L.logDebug(f'Announce resource: {resource.ri} to: {csi}')
if self._isResourceAnnouncedTo(resource, csi):
L.isDebug and L.logDebug(f'resource already announced: {resource.ri}')
return
# Create announced resource & type
dct = resource.createAnnouncedResourceDict(isCreate = True)
tyAnnc = ResourceTypes(resource.ty).announced()
targetID = ''
if resource.ty != ResourceTypes.CSEBase: # CSEBase is just announced below
if not (at := resource.at):
L.isDebug and L.logDebug('at attribute is empty.')
return # Not much to do here
# Check if parent is announced already to the same remote CSE
try:
parentResource = CSE.dispatcher.retrieveLocalResource(resource.pi)
except ResponseException as e:
raise INTERNAL_SERVER_ERROR(L.logErr(f'cannot retrieve parent. Announcement not possible: {e.dbg}'))
# For announcing the CSEBase we want to take some extra care and check whether it really
# is available at the remote CSE. It could have been removed (expiration, restart, ...) and
# this may not be reflected
if parentResource.ty == ResourceTypes.CSEBase:
checkCSEBaseAnnouncement(parentResource) # type:ignore[arg-type]
parentResource.dbReload() # parent is already the CSEBase, just reload from DB
else: # parent is not a CSEBase
if not self._isResourceAnnouncedTo(parentResource, csi):
L.isDebug and L.logDebug(f'parent resource is not announced: {parentResource.ri}')
# parent resource is not announced -> announce the resource directly under the CSEBaseAnnc
# Don't allow instances to be announced without their parents
if resource.ty in [ResourceTypes.CIN, ResourceTypes.FCI, ResourceTypes.TSI]:
L.logWarn('Announcing instances without their parents is not allowed. Unsuccessful announcement')
return
# Whatever the parent resource is, check whether the CSEBase has been announced. Announce it if necessay
# and set the announced CSEBase as new parent
checkCSEBaseAnnouncement(parentResource := getCSE())
# ... then continue with normale announcement of the resource. The parent for the announcement is now the CSEBase
# parent resource is announced -> Announce the resource under the parent resource Annc
if not (at := self._announcedInfos(parentResource, csi)):
raise BAD_REQUEST(L.logWarn(f'no announcement for parent resource: {parentResource.ri} to: {csi}'))
targetID = at[1]
# Create the announed resource on the remote CSE
if targetID:
to = targetID if isSPRelative(targetID) else f'{csi}/{targetID}'
else:
# We don't know the name of the remote CSEBase, so we have to use the CSI + '-'
to = f'{csi}/-'
L.isDebug and L.logDebug(f'creating announced resource at: {to}')
try:
res = CSE.request.handleSendRequest(CSERequest(op = Operation.CREATE,
to = to,
originator = CSE.cseCsi,
ty = tyAnnc,
pc = dct)
)[0].result # there should be at least one result
if res.rsc == ResponseStatusCode.CREATED:
resource.addAnnouncementToResource(csi, findXPath(cast(JSON, res.data), '{*}/ri'))
L.isDebug and L.logDebug(f'Announced resource created: {resource.getAnnouncedTo()}')
resource.dbUpdate()
else:
L.isWarn and L.logWarn(f'Announced resource could not be created at: {to} ({res.rsc})')
except ResponseException as e:
e.dbg = L.logDebug(f'Error creating remote announced resource: {int(e.rsc)} ({e.dbg})')
raise e
#
# De-Announcements
#
def checkResourcesForUnAnnouncement(self, remoteCSR:Resource) -> None:
""" Check whether resources need announcements, and initiate announcement
if they do.
Args:
remoteCSR: The `CSR` remote resource.
See also:
- searchAnnounceableResourcesForCSI
"""
csi = remoteCSR.csi
L.isDebug and L.logDebug(f'Checking resources for Unannouncement to: {csi}')
# get all reources for this specific CSI that are NOT announced to it yet
resources = self.searchAnnounceableResourcesForCSI(csi, True)
# try to announce all not-announced resources to this csr
for resource in resources:
self.deAnnounceResource(resource)
self._removeAnnouncementFromResource(resource, csi)
resource.dbUpdate()
def deAnnounceResource(self, resource:AnnounceableResource) -> None:
""" De-announce a single resource from its announcement target(s).
Args:
resource: The announceable resource to de-announce.
See also:
- deAnnounceResourceFromCSI
"""
L.isDebug and L.logDebug(f'De-Announce resource: {resource.ri} from all connected csr')
for (csi, remoteRI) in resource.getAnnouncedTo():
self.deAnnounceResourceFromCSI(resource, csi, remoteRI)
def deAnnounceResourceFromCSI(self, resource:AnnounceableResource, csi:str, remoteRI:str) -> None:
""" De-Announce a resource from a specific `CSR`.
Args:
resource: The announceable resource to de-announce.
csi: The CSE-ID of the CSE from which the resource is to be de-announced.
remoteRI: The resource ID of the remote announced resource.
"""
# Delete the announed resource from the remote CSE
csrID = f'{csi}/{remoteRI}'
L.isDebug and L.logDebug(f'Delete announced resource: {csrID}')
res = CSE.request.handleSendRequest(CSERequest(op = Operation.DELETE,
to = csrID,
originator = CSE.cseCsi))[0].result # there should be at least one result
if res.rsc not in [ ResponseStatusCode.DELETED, ResponseStatusCode.OK ]:
L.isWarn and L.logWarn(f'Error deleting remote announced resource: {res.rsc}')
# ignore the fact that we cannot delete the announced resource.
# fall-through for some house-keeping
self._removeAnnouncementFromResource(resource, csi)
L.isDebug and L.logDebug('Announced resource deleted')
resource.dbUpdate()
#
# Update Announcements
#
def announceUpdatedResource(self, resource:AnnounceableResource, originator:str) -> None:
""" (Newly) announce an updated resource to a remote CSE.
Args:
resource: The announceable resource that has been updated.
originator: The original UPDATE request's originator.
"""
L.isDebug and L.logDebug(f'Updating announced resource: {resource.ri}')
# get all resources for this specific CSI that are announced to it yet
CSIsFromAnnounceTo = []
for announcedResourceID in resource.at:
if len(sp := announcedResourceID.split('/')) >= 2:
if (csi := f'/{sp[1]}') == CSE.cseCsi or csi.startswith(f'{CSE.cseCsi}/'): # Ignore own CSE as target
continue
CSIsFromAnnounceTo.append(csi)
# Update the annoucned remote resources
announcedCSIs = []
for (csi, remoteRI) in resource.getAnnouncedTo():
if csi == originator: # Skip the announced resource at the originator !!
continue
announcedCSIs.append(csi) # build a list of already announced CSIs
self.updateResourceOnCSI(resource, csi, remoteRI)
# Check for any non-announced csi in at, and possibly announce them
for csi in CSIsFromAnnounceTo:
if csi not in announcedCSIs:
self.announceResourceToCSI(resource, csi)
def updateResourceOnCSI(self, resource:AnnounceableResource, csi:str, remoteRI:str) -> None:
""" Update an announced resource on a specific remote CSE.
Args:
resource: The announceable resource to update.
csi: The CSE-ID of the CSE where the announced resource is hosted.
remoteRI: The resource ID of the remote announced resource.
"""
dct = resource.createAnnouncedResourceDict(isCreate = False)
# Create the announed resource on the remote CSE
csrID = f'{csi}/{remoteRI}'
L.isDebug and L.logDebug(f'Updating announced resource at: {csrID}')
res = CSE.request.handleSendRequest(CSERequest(op = Operation.UPDATE,
to = csrID,
originator = CSE.cseCsi,
pc = dct))[0].result # there should be at least one result
if res.rsc not in [ ResponseStatusCode.UPDATED, ResponseStatusCode.OK ]:
L.isDebug and L.logDebug(f'Error updating remote announced resource: {int(res.rsc)}')
# Ignore and fallthrough
L.isDebug and L.logDebug('Announced resource updated')
def _removeAnnouncementFromResource(self, resource:Resource, csi:str) -> None:
""" Remove announcement details from a resource.
Modify the internal *__announcedTo__* attribute as well the *at* attribute
to remove the reference to the remote CSE from announced resource.
Args:
resource: The announceable resource to remove.
csi: The CSE-ID of the CSE where the announced resource is hosted.
"""
remoteRI = resource.removeAnnouncementFromResource(csi)
# Modify the at attribute
if remoteRI:
atCsi = f'{csi}/{remoteRI}'
if (at := resource.at) and atCsi in at:
at.remove(atCsi)
resource.setAttribute('at', at)
def _isResourceAnnouncedTo(self, resource:Resource, csi:str) -> bool:
""" Check whether a resource is announced to a specific remote CSE.
This is done by looking at the entries in the internal *__announcedTo__*
attribute, ie. whether they will contain the *csi* of the remote CSE.
Args:
resource: The announceable resource to check.
csi: The CSE-ID of the CSE where the announced resource is supposed to be hosted.
Return:
Boolean indicating the announced status.
"""
return (at := resource.getAnnouncedTo()) is not None and any(csi == _csi for (_csi, _) in at)
def _announcedInfos(self, resource:Resource, csi:str) -> Optional[Tuple[str, str]]:
""" Return the matching tuple for the given *csi* of a resource announcement,
or *None* if none is set.
Args:
resource: The announceable resource to check.
csi: The CSE-ID of the CSE where the announced resource is supposed to be hosted.
"""
if at := resource.getAnnouncedTo():
for _at in at:
if _at[0] == csi:
return _at
return None
def announceResourceViaDirectURL(self, resource:Resource, at:str) -> bool:
""" Announce a resource via a direct URL, nit via a csi.
Attention:
Not supported yet.
Args:
resource: The announceable resource to announce.
at: The direct URL of the remote CSE to where to announce the resource.
Return:
Boolean indicating the result.
"""
L.logErr('TODO Direct Announcement')
return False
#########################################################################
#
# Utilities
#
def searchAnnounceableResourcesForCSI(self, csi:str, isAnnounced:bool) -> list[AnnounceableResource]:
""" Search and retrieve all resources that have the provided CSI in their
*at* attribute.
Also distinguish between announced and not announced resources in the filter.
Args:
csi: The CSE-ID of the CSE for which the announced resource are searched.
isAnnounced: Boolean indicating whether announced or non-announced resources are searched for.
Return:
List of `AnnounceableResource` resources that have been found.
"""
mcsi = f'{csi}/'
def _announcedFilter(r:JSON) -> bool:
""" Internal filter function for announced resources.
Args:
r: Resource to check.
Return:
Boolean indicating the search filter result.
"""
if (at := r.get('at')) and len(list(filter(lambda x: x.startswith(mcsi), at))) > 0: # check whether any entry in 'at' startswith mcsi
if ato := r.get(Constants.attrAnnouncedTo):
for i in ato:
if csi == i[0]: # 0=remote csi,
return isAnnounced
return not isAnnounced
return False
return cast(List[AnnounceableResource], CSE.storage.searchByFilter(_announcedFilter))