/
resource_lcs_policy.py
582 lines (419 loc) · 22.2 KB
/
resource_lcs_policy.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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
#!/usr/bin/env python
"""
@package ion.services.sa.instrument.policy
@author Ian Katz
"""
from pyon.public import PRED, RT, LCS
from pyon.ion.resource import get_maturity_visibility
from ion.services.sa.instrument.flag import KeywordFlag
from ooi.logging import log
class ResourceLCSPolicy(object):
def __init__(self, clients):
self.clients = clients
if hasattr(clients, "resource_registry"):
self.RR = self.clients.resource_registry
self.on_policy_init()
def on_policy_init(self):
pass
def _make_result(self, result, message):
if result:
return True, ""
else:
return False, message
def _make_pass(self):
return self._make_result(True, "")
def _make_fail(self, message):
return self._make_result(False, message)
def _make_warn(self, message):
return self._make_result(True, message)
def _get_resource_type_by_id(self, resource_id):
"""
get the type of a resource by id
@param resource_id a resource id
"""
assert(type("") == type(resource_id))
resource = self.RR.read(resource_id)
return resource._get_type()
def _find_having(self, subject_type, association_predicate, some_object_id):
"""
find resources having ____:
find resource IDs of the predefined type that
have the given association attached
@param association_predicate one of the association types
@param some_object the object "owned" by the association type
"""
assert(type("") == type(some_object_id))
ret, _ = self.RR.find_subjects(subject_type,
association_predicate,
some_object_id,
False)
return ret
def _find_stemming(self, primary_object_id, association_predicate, some_object_type):
"""
find resources stemming from _____:
find resource IDs of the given object type that
are associated with the primary object
@param primary_object_id the id of the primary object
@param association_predicate the association type
@param some_object_type the type of associated object
"""
assert(type("") == type(primary_object_id))
ret, _ = self.RR.find_objects(primary_object_id,
association_predicate,
some_object_type,
False)
return ret
def _has_keyworded_attachment(self, resource_id, desired_keyword):
assert(type("") == type(resource_id))
for a in self._find_stemming(resource_id, PRED.hasAttachment, RT.Attachment):
for k in a.keywords:
if desired_keyword == k:
return self._make_pass()
return self._make_fail("No attachment found with keyword='%s'" % desired_keyword)
def _resource_lcstate_in(self, resource_obj, permissible_states=None):
assert(type("") != type(resource_obj))
if permissible_states is None:
permissible_states = []
parts = get_maturity_visibility(resource_obj.lcstate)
return self._make_result(parts[0] in permissible_states,
"'%s' resource is in state '%s', wanted [%s]" %
(resource_obj._get_type(), parts[0], str(permissible_states)))
class AgentPolicy(ResourceLCSPolicy):
def lce_precondition_plan(self, agent_id):
# always OK
return self._make_pass()
def lce_precondition_develop(self, agent_id):
former = self.lce_precondition_plan(agent_id)
if not former[0]: return former
agent_type = self._get_resource_type_by_id(agent_id)
if RT.InstrumentAgent == agent_type:
return self._make_result(0 < len(self._find_stemming(agent_id, PRED.hasModel, RT.InstrumentModel)),
"No model associated with agent")
if RT.PlatformAgent == agent_type:
return self._make_result(0 < len(self._find_stemming(agent_id, PRED.hasModel, RT.PlatformModel)),
"No model associated with agent")
return self._make_fail("Wrong agent resource type (got '%s')" % agent_type)
def lce_precondition_integrate(self, agent_id):
former = self.lce_precondition_develop(agent_id)
if not former[0]: return former
#if not checking platform agents yet, uncomment this
#if RT.PlatformAgent == self._get_resource_type_by_id(agent_id): return True
return self._has_keyworded_attachment(agent_id, KeywordFlag.EGG_URL)
def lce_precondition_deploy(self, agent_id):
former = self.lce_precondition_integrate(agent_id)
if not former[0]: return former
#if no checking platform agents yet, uncomment this
#if RT.PlatformAgent == self._get_resource_type_by_id(agent_id): return True
return self._has_keyworded_attachment(agent_id, KeywordFlag.CERTIFICATION)
def lce_precondition_retire(self, agent_id):
ret = (0 == self._find_having(RT.InstrumentAgentInstance, PRED.hasAgentDefinition, agent_id))
return self._make_result(ret, "InstrumentAgentInstance(s) are still using this InstrumentAgent")
def precondition_delete(self, agent_id):
ret = (0 == self._find_having(RT.InstrumentAgentInstance, PRED.hasAgentDefinition, agent_id))
return self._make_result(ret, "InstrumentAgentInstance(s) are still using this InstrumentAgent")
class ModelPolicy(ResourceLCSPolicy):
def lce_precondition_plan(self, model_id):
# always OK
return self._make_pass()
def lce_precondition_develop(self, model_id):
# todo
return self._make_pass()
def lce_precondition_integrate(self, model_id):
# todo
return self._make_pass()
def lce_precondition_deploy(self, model_id):
# todo
return self._make_pass()
def lce_precondition_retire(self, model_id):
# todo: more than checking agents, devices, and sites?
model_type = self._get_resource_type_by_id(model_id)
if RT.SensorModel == model_type:
return 0 == len(self._find_having(RT.SensorDevice, PRED.hasModel, model_id))
if RT.InstrumentModel == model_type:
if 0 < len(self._find_having(RT.InstrumentDevice, PRED.hasModel, model_id)):
return self._make_fail("InstrumentDevice(s) are using this model")
if 0 < len(self._find_having(RT.InstrumentAgent, PRED.hasModel, model_id)):
return self._make_fail("InstrumentAgent(s) are using this model")
if 0 < len(self._find_having(RT.InstrumentSite, PRED.hasModel, model_id)):
return self._make_fail("InstrumentSite(s) are using this model")
return self._make_pass()
if RT.PlatformModel == model_type:
if 0 < len(self._find_having(RT.PlatformDevice, PRED.hasModel, model_id)):
return self._make_fail("PlatformDevice(s) are using this model")
if 0 < len(self._find_having(RT.PlatformAgent, PRED.hasModel, model_id)):
return self._make_fail("PlatformAgent(s) are using this model")
if 0 < len(self._find_having(RT.PlatformSite, PRED.hasModel, model_id)):
return self._make_fail("PlatformSite(s) are using this model")
return self._make_pass()
return self._make_fail("Wrong model resource type (got '%s')" % model_type)
def precondition_delete(self, model_id):
model_type = self._get_resource_type_by_id(model_id)
if RT.SensorModel == model_type:
return 0 == len(self._find_having(RT.SensorDevice, PRED.hasModel, model_id))
if RT.InstrumentModel == model_type:
if 0 < len(self._find_having(RT.InstrumentDevice, PRED.hasModel, model_id)):
return self._make_fail("InstrumentDevice(s) are using this model")
if 0 < len(self._find_having(RT.InstrumentAgent, PRED.hasModel, model_id)):
return self._make_fail("InstrumentAgent(s) are using this model")
if 0 < len(self._find_having(RT.InstrumentSite, PRED.hasModel, model_id)):
return self._make_fail("InstrumentSite(s) are using this model")
return self._make_pass()
if RT.PlatformModel == model_type:
if 0 < len(self._find_having(RT.PlatformDevice, PRED.hasModel, model_id)):
return self._make_fail("PlatformDevice(s) are using this model")
if 0 < len(self._find_having(RT.PlatformAgent, PRED.hasModel, model_id)):
return self._make_fail("PlatformAgent(s) are using this model")
if 0 < len(self._find_having(RT.PlatformSite, PRED.hasModel, model_id)):
return self._make_fail("PlatformSite(s) are using this model")
return self._make_pass()
return self._make_fail("Wrong model resource type (got '%s')" % model_type)
class DevicePolicy(ResourceLCSPolicy):
def invalid_custom_attrs(self, device_id, model_id):
assert(type("") == type(device_id) == type(model_id))
model_obj = self.RR.read(model_id)
device_obj = self.RR.read(device_id)
bad = {}
for k, v in device_obj.custom_attributes.iteritems():
if not k in model_obj.custom_attributes:
bad[k] = v
if {} == bad:
return ""
else:
return "Device contains custom attributes undefined by associated model: %s" % str(bad)
def lce_precondition_plan(self, device_id):
obj = self.RR.read(device_id)
return self._make_result("" != obj.name, "Name was not defined")
def lce_precondition_develop(self, device_id):
former = self.lce_precondition_plan(device_id)
if not former[0]: return former
#have an agent/deployed, model/deployed
obj = self.RR.read(device_id)
device_type = self._get_resource_type_by_id(device_id)
if "" == obj.serial_number:
return self._make_fail("Device has no serial number")
if RT.InstrumentDevice == device_type:
models = self._find_stemming(device_id, PRED.hasModel, RT.InstrumentModel)
if 0 == len(models):
return self._make_fail("Device has no associated model")
if not self._resource_lcstate_in(models[0], [LCS.DEPLOYED]):
return self._make_fail("Device's associated model is not in '%s'" % LCS.DEPLOYED)
#validate custom fields
bad = self.invalid_custom_attrs(device_id, models[0]._id)
if "" != bad:
return self._make_fail(bad)
return self._has_keyworded_attachment(device_id, KeywordFlag.VENDOR_TEST_RESULTS)
if RT.PlatformDevice == device_type:
models = self._find_stemming(device_id, PRED.hasModel, RT.PlatformModel)
if 0 == len(models):
return self._make_fail("Device has no associated model")
if not self._resource_lcstate_in(models[0], [LCS.DEPLOYED]):
return self._make_fail("Device's associated model is not in '%s'" % LCS.DEPLOYED)
#validate custom fields
bad = self.invalid_custom_attrs(device_id, models[0]._id)
if "" != bad:
return self._make_fail(bad)
return self._has_keyworded_attachment(device_id, KeywordFlag.VENDOR_TEST_RESULTS)
return self._make_fail("Wrong device resource type (got '%s')" % device_type)
def lce_precondition_integrate(self, device_id):
former = self.lce_precondition_develop(device_id)
if not former[0]: return former
#Have an instrument site/deployed, site has agent, site agent == device agent
device_type = self._get_resource_type_by_id(device_id)
if RT.PlatformDevice == device_type:
agents = self._find_stemming(device_id, PRED.hasAgentInstance, RT.PlatformAgentInstance)
if 0 == len(agents):
return self._make_fail("Device has no associated agent instance")
tmp = self._resource_lcstate_in(agents[0], [LCS.DEPLOYED])
if not tmp[0]: return tmp
return self._make_pass()
if RT.InstrumentDevice == device_type:
agents = self._find_stemming(device_id, PRED.hasAgentInstance, RT.InstrumentAgentInstance)
if 0 == len(agents):
return self._make_fail("Device has no associated agent instance")
tmp = self._resource_lcstate_in(agents[0], [LCS.DEPLOYED])
if not tmp[0]: return tmp
parents = self._find_having(RT.PlatformDevice, PRED.hasDevice, device_id)
if 0 == len(parents):
return self._make_fail("Device is not attached to a parent")
tmp = self._resource_lcstate_in(parents[0], [LCS.INTEGRATED, LCS.DEPLOYED])
if not tmp[0]: return tmp
return self._make_pass()
if RT.SensorDevice == device_type:
parents = self._find_having(RT.InstrumentDevice, PRED.hasDevice, device_id)
if 0 == len(parents):
return self._make_fail("Device is not attached to a parent")
tmp = self._resource_lcstate_in(parents[0], [LCS.INTEGRATED, LCS.DEPLOYED])
if not tmp[0]: return tmp
return self._make_pass()
#todo: verify comms with device??
return self._make_fail("Wrong device resource type (got '%s')" % device_type)
def lce_precondition_deploy(self, device_id):
former = self.lce_precondition_integrate(device_id)
if not former[0]: return former
# Have associated agent instance, has a parent subsite which is deployed, platform device has platform site, all deployed.
device_type = self._get_resource_type_by_id(device_id)
if RT.SensorDevice == device_type:
parents = self._find_having(RT.InstrumentDevice, PRED.hasDevice, device_id)
if 0 == len(parents):
return self._make_fail("Device is not attached to a parent")
tmp = self._resource_lcstate_in(parents[0], [LCS.DEPLOYED])
if not tmp[0]: return tmp
return self._make_pass()
if RT.InstrumentDevice == device_type:
parents = self._find_having(RT.PlatformDevice, PRED.hasDevice, device_id)
if 0 == len(parents):
return self._make_fail("Device is not attached to a parent")
tmp = self._resource_lcstate_in(parents[0], [LCS.DEPLOYED])
if not tmp[0]: return tmp
sites = self._find_having(RT.InstrumentSite, PRED.hasDevice, device_id)
if 0 == len(sites):
return self._make_fail("Device does not have an assigned site")
tmp = self._resource_lcstate_in(sites[0], [LCS.DEPLOYED])
if not tmp[0]: return tmp
return self._make_pass()
if RT.PlatformDevice == device_type:
sites = self._find_having(RT.PlatformSite, PRED.hasDevice, device_id)
if 0 == len(sites):
return self._make_fail("Device does not have an assigned site")
tmp = self._resource_lcstate_in(sites[0], [LCS.DEPLOYED])
if not tmp[0]: return tmp
#todo: add check that model is supported
return self._make_pass()
return self._make_fail("Wrong device resource type (got '%s')" % device_type)
def lce_precondition_retire(self, device_id):
device_type = self._get_resource_type_by_id(device_id)
if RT.SensorDevice == device_type:
return self._make_pass()
if RT.InstrumentDevice == device_type:
if 0 < len(self._find_having(RT.InstrumentSite, PRED.hasDevice, device_id)):
return self._make_fail("Device is still assigned to a site")
if 0 < len(self._find_stemming(device_id, PRED.hasDeployment, RT.Deployment)):
return self._make_fail("Device is still assigned to a deployment")
if RT.PlatformDevice == device_type:
if 0 < len(self._find_having(RT.PlatformSite, PRED.hasDevice, device_id)):
return self._make_fail("Device is still assigned to a site")
if 0 < len(self._find_stemming(device_id, PRED.hasDeployment, RT.Deployment)):
return self._make_fail("Device is still assigned to a deployment")
return self._make_fail("Wrong device resource type (got '%s')" % device_type)
def precondition_delete(self, device_id):
return self.lce_precondition_retire(device_id)
class SitePolicy(ResourceLCSPolicy):
def lce_precondition_plan(self, site_id):
# always OK
return self._make_pass()
def lce_precondition_develop(self, site_id):
former = self.lce_precondition_plan(site_id)
if not former[0]: return former
# todo
return self._make_pass()
def lce_precondition_integrate(self, site_id):
former = self.lce_precondition_develop(site_id)
if not former[0]: return former
# todo
return self._make_pass()
def lce_precondition_deploy(self, site_id):
former = self.lce_precondition_integrate(site_id)
if not former[0]: return former
# todo
return self._make_pass()
def lce_precondition_retire(self, site_id):
# todo: Sites and all subclasses can not be retired if they have children or if they are
# not associated to a deployment
site_type = self._get_resource_type_by_id(site_id)
if RT.InstrumentSite == site_type:
if 0 < len(self._find_stemming(site_id, PRED.hasDeployment, RT.Deployment)):
return self._make_fail("Site is still assigned to a deployment")
if RT.PlatformSite == site_type:
if 0 < len(self._find_stemming(site_id, PRED.hasSite, RT.PlatformDevice)):
return self._make_fail("Device is still assigned a child platform site")
if 0 < len(self._find_stemming(site_id, PRED.hasDevice, RT.InstrumentDevice)):
return self._make_fail("Device is still hasSite a child instrument site")
if 0 < len(self._find_stemming(site_id, PRED.hasDeployment, RT.Deployment)):
return self._make_fail("Site is still assigned to a deployment")
return self._make_fail("Wrong device resource type (got '%s')" % site_type)
def precondition_delete(self, site_id):
return self.lce_precondition_retire(site_id)
class DataProductPolicy(ResourceLCSPolicy):
def lce_precondition_plan(self, data_product_id):
# always OK
return self._make_pass()
def lce_precondition_develop(self, data_product_id):
former = self.lce_precondition_plan(data_product_id)
if not former[0]: return former
# todo: mandatory fields?
return self._has_keyworded_attachment(data_product_id, KeywordFlag.CERTIFICATION)
def lce_precondition_integrate(self, data_product_id):
former = self.lce_precondition_develop(data_product_id)
if not former[0]: return former
if 0 == len(self._find_stemming(data_product_id, PRED.hasStream, RT.Stream)):
return self._make_fail("Product has no associated stream")
pducers = self._find_stemming(data_product_id, PRED.hasDataProducer, RT.DataProducer)
if 0 == len(pducers):
return self._make_fail("Product has no associated producer")
if 0 < len(self._find_stemming(pducers[0], PRED.hasInputDataProducer, RT.DataProducer)):
return self._make_pass()
elif 0 < len(self._find_stemming(pducers[0], PRED.hasOutputDataProducer, RT.DataProducer)):
return self._make_pass()
else:
return self._make_fail("Product's producer has neither input nor output data producer")
def lce_precondition_deploy(self, data_product_id):
former = self.lce_precondition_integrate(data_product_id)
if not former[0]: return former
datasets = self._find_stemming(data_product_id, PRED.hasDataset, RT.Dataset)
if 0 == len(datasets):
return self._make_warn("Dataset not available")
else:
return self._resource_lcstate_in(datasets[0])
def lce_precondition_retire(self, data_product_id):
# todo:
return self._make_pass()
class DataProcessPolicy(ResourceLCSPolicy):
def lce_precondition_plan(self, data_process_id):
# always OK
return self._make_pass()
def lce_precondition_develop(self, data_process_id):
former = self.lce_precondition_plan(data_process_id)
if not former[0]: return former
# todo: required fields
return self._make_pass()
def lce_precondition_integrate(self, data_process_id):
former = self.lce_precondition_develop(data_process_id)
if not former[0]: return former
# todo: currently, nothing. "MAY be assoc with QA test results"
return self._make_pass()
def lce_precondition_deploy(self, data_process_id):
former = self.lce_precondition_integrate(data_process_id)
if not former[0]: return former
#todo: something about python egg, not sure yet
return self._has_keyworded_attachment(data_process_id, KeywordFlag.CERTIFICATION)
def lce_precondition_retire(self, data_process_id):
# todo:
return self._make_pass()
# currently same as DataProcess
class DataProcessDefinitionPolicy(DataProcessPolicy):
pass
"""
# in case i need another one
class XxPolicy(Policy):
def lce_precondition_plan(self, x_id):
# always OK
return self._make_pass()
def lce_precondition_develop(self, x_id):
former = self.lce_precondition_plan(x_id)
if not former[0]: return former
# todo
return self._make_pass()
def lce_precondition_integrate(self, x_id):
former = self.lce_precondition_develop(x_id)
if not former[0]: return former
# todo
return self._make_pass()
def lce_precondition_deploy(self, x_id):
former = self.lce_precondition_integrate(x_id)
if not former[0]: return former
# todo
return self._make_pass()
def lce_precondition_retire(self, x_id):
# todo:
return self._make_pass()
"""