-
Notifications
You must be signed in to change notification settings - Fork 26
/
_subscriptionproviders.py
862 lines (677 loc) · 35.1 KB
/
_subscriptionproviders.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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
#
# (C) Copyright 2020 InovaDevelopment.com
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Author: Karl Schopmeyer <inovadevelopment.com>
#
"""
This module implements user providerw for the classes required to create and
manage CIM indications subscriptions including: CIM_ListenerDestinationCIMXML,
CIM_IndicationFilter, and CIM_IndicationSubscription. These providers are
packaged as single file because they are all required together.
This provider manages creation and deletion of the instances of the
These 3 classes and limits that activity to the server
environment interop namespace.
The user providers extend the basic Create, Modify, Delete functionality by:
1. Adding properties to the instances (ex. PersistenceType as the WBEM
server is expected to do)
2. Complete the key properties of SystemName, etc. which the provider does
not know.
3. Validate that instances are only created in the interop namespace.
4. Validate that reference parameters do exist for CreateInstances and that
They do not exist for DeleteInstances. Ex. You cannot delete a filter that
is included in a subscription.
"""
import six
from pywbem import CIMError, CIM_ERR_NOT_SUPPORTED, \
CIM_ERR_INVALID_PARAMETER, CIM_ERR_FAILED, Uint16, CIMDateTime, \
CIMProperty
from pywbem._utils import _format
from ._instancewriteprovider import InstanceWriteProvider
from .config import SYSTEMNAME, SYSTEMCREATIONCLASSNAME
__all__ = ['CIMIndicationFilterProvider', 'CIMListenerDestinationProvider',
'CIMIndicationSubscriptionProvider']
# CIM class name of the classes implemented in these providers
SUBSCRIPTION_CLASSNAME = 'CIM_IndicationSubscription'
FILTER_CLASSNAME = 'CIM_IndicationFilter'
LISTENERDESTINATION_CLASSNAME = 'CIM_ListenerDestinationCIMXML'
def set_property(instance, name, value, conditional=True):
"""
Add a new property and value to instance or modify value if property
exists. If conditional is True, only add if property does not exist.
Otherwise, always set new value into property.
"""
if conditional:
if name not in instance:
instance[name] = value
else:
instance[name] = value
class CommonMethodsMixin(object):
"""
Common Methods and functionality for the 3 providers defined to support
indication subscriptions
"""
def validate_modify_instance(self, modified_instance,
modifiable_properties=None,
IncludeQualifiers=None):
# pylint: disable=invalid-name
"""
Common code for ModifyInstance method for subscription providers.
This code validates that the modify instance is allowed and validates
that the properties to be modified are the ones allowed in the
modified_instance.
This method assumes that:
- The provider method is called only for the registered class and
namespace (only applies to user-defined providers).
- The Python types of all input parameters to this provider method are
as specified below.
- The classnames in modified_instance are consistent between
instance and instance path.
- The namespace exists in the CIM repository.
- The creation class of the instance to be modified exists in the
namespace of the CIM repository.
- The instance to be modified exists in the namespace of the CIM
repository.
- All properties in modified_instance that are to be modified are
exposed (i.e. defined and inherited with any overrides resolved) by
the creation class in the CIM repository, and have the same
type-related attributes (i.e. type, is_array, embedded_object).
- No key properties are requested to change their values.
Validation that should be performed by this provider method:
- modified_instance does not specify any changed values for
properties that are not allowed to be changed by the client,
depending on the model implemented by the provider.
Parameters:
modified_instance (:class:`~pywbem.CIMInstance`):
A representation of the modified CIM instance, also indicating its
instance path, with exactly the set of properties to be modified.
This object is a deep copy of the original client parameter, and may
be modified by the provider as needed, before storing it in the
CIM repository.
The `path` attribute of this object will be set and is the
instance path of the instance to be modified in the CIM repository.
Its `namespace`, `classname` and `keybindings` attributes
will be set. The names will be in any lexical case.
The `classname` attribute of this object will specify the creation
class of the instance to be modified, in any lexical case.
The `properties` attribute of this object will specify exactly the
set of properties that are to be updated, taking into account the
original ModifiedInstance and PropertyList input parameters of the
ModifyInstance() client call.
The lexical case of the property names has been adjusted to match
the lexical cae of the property definitions in the creation class
in the CIM repository.
The `qualifiers` attribute of this object, if non-empty, should
be ignored by the provider, because instance-level qualifiers have
been deprecated in CIM.
modifiable_properties (:term:`string` or list of :term:`string`):
Property names of properties that are modifiable by. If
properties that are not modifiable are included, the request
is rejected with CIM_ERR_INVALID_PARAMETER
Raises:
CIMError exception if either the modification not allowed
(supported_properties is None) or the properties in the modified
instance are not included in the modifiable_properties list
"""
if not modifiable_properties:
raise CIMError(
CIM_ERR_NOT_SUPPORTED,
_format("Modification of {0} instances is not allowed: "
"{1!A}",
modified_instance.classname, modified_instance.path))
inst_properties = modified_instance.keys()
invalid_props = [pname for pname in inst_properties if pname not in
modifiable_properties]
if IncludeQualifiers:
raise CIMError(
CIM_ERR_INVALID_PARAMETER,
_format("Modification of qualifiers is for {0}"
"not allowed.", modified_instance.classname))
if invalid_props:
raise CIMError(
CIM_ERR_INVALID_PARAMETER,
_format("Modification of properties {0} instance of {1!A} is "
"not allowed: {2!A}", ", ".join(invalid_props),
modified_instance.classname, modified_instance.path))
if modified_instance.classname != SUBSCRIPTION_CLASSNAME:
self.validate_no_subscription(modified_instance.path)
def parameter_is_interop(self, ns, classname):
# pylint: disable=no-self-use
"""
Test if the parameter provided in ns is the interop namespace
"""
if not self.is_interop_namespace(ns):
raise CIMError(
CIM_ERR_INVALID_PARAMETER,
_format(
"Cannot create instance of class {0!A} in namespace {1!A}: "
"The namespace is not an Interop namespace. "
"Valid Interop namespaces are: {2!A}",
classname, ns, ", ".join(self.interop_namespace_names)))
def fix_key_properties(self, new_instance):
# pylint: disable=no-self-use
"""
Fix the key properties that are common to many classes (SystemName,
CreationClassName, SystemCreationClassName) since these may be
either not provided, None, or incorrect value for this environment with
the new instance, they may be overridden. This works only for
the filter and destination classes that contain the same set of
key properties.
"""
ccn_pname = 'CreationClassName'
sys_pname = 'SystemName'
sccn_pname = 'SystemCreationClassName'
# Validate or fix other key values in the new instance
def _fix_key_prop(pname, test_value, replacement=None):
"""
Replace property pname with either replacement if replacement not
None or test_value if property does not exist, is None, or not same
value as test_value
"""
if pname not in new_instance or new_instance[pname] is None or \
new_instance[pname].lower != test_value.lower:
new_instance[pname] = replacement or test_value
# Set the keys to default if they don't exist are None, or have
# invalid value
_fix_key_prop(ccn_pname, new_instance.classname,
new_instance.classname)
_fix_key_prop(sys_pname, SYSTEMNAME)
_fix_key_prop(sccn_pname, SYSTEMCREATIONCLASSNAME)
def validate_required_properties_exist(self, new_instance, namespace,
required_properties):
# pylint: disable=no-self-use
"""
Validate that the properties in required_properties list do exist
in the new_instance
"""
for pn in required_properties:
if pn not in new_instance:
raise CIMError(
CIM_ERR_INVALID_PARAMETER,
_format(
"Cannot create instance of class {0!A} in "
"namespace {1!A}: "
"Missing required property {2!A} in new_instance",
new_instance.classname, namespace, pn))
def validate_no_subscription(self, instance_name):
"""
Validate that no subscriptions exist containing reference to this
instance.
Parameters:
instance_name (:class:`~pywbem.CIMInstanceName`)
Instance name of the target instance. The class must be either
the filter class or the listener destination class
Returns:
Returns if there are no corresponding subscriptions.
"""
# If a subscription exists containing this ListenerDestination,
# reject delete
if self.conn.ReferenceNames(instance_name,
ResultClass=SUBSCRIPTION_CLASSNAME):
# DSP1054 1.2 defines CIM error is raised by the server
# in that case; we simulate it.
raise CIMError(
CIM_ERR_FAILED,
_format("The instance {0} is referenced by "
"subscriptions.", instance_name),
conn_id=self.conn.conn_id)
def post_register_setup(self, conn):
"""
Method called by FakedWBEMConnection.register_provider to complete
initialization of this provider. This method is called after
the required classes are installed in the cim_repository
This is necessary because pywbem_mock does not allow user-defined
providers for the instance read operations such as EnumerateInstances
so the instance for each namespace must be exist in the repository.
This method is common to all of the subscription providers.
This method:
1. Validates that the provider classs exist.
"""
assert self.installed is False
self.installed = True
# Validate provider classes installed. Will generate exception
# if class does not exist.
interop_namespace = conn.find_interop_namespace()
if isinstance(self.provider_classnames, six.string_types):
clns = [self.provider_classnames]
else:
clns = self.provider_classnames
for cln in clns:
conn.GetClass(cln, interop_namespace)
self.conn = conn
class CIMIndicationFilterProvider(CommonMethodsMixin, InstanceWriteProvider):
# pylint: disable=line-too-long
"""
Implements the user defined provider for the class CIM_IndicationFilter.
This provider provides the create, modify, and delete methods for
adding an instance of the class CIM_Namspace when a namespace is created or
deleted in a pywbem mock environment.
This class and the instances of this class only exist in the WBEM server
Interop namespace per DMTF definitions.
This class __init__ saves the cimrepository variable used by methods in
this class and by methods of its superclasses (i.e. InstanceWriteProvider).
the provider defines the class level attribute `provider_classnames`
(cim_indicationfilter)
This provider presumes that an Interop namespace has been created before
the provider object is constructed and fails the constructor if
there is not interop_namespace
""" # noqa: E501
# pylint: enable=line-too-long
# This class level attribute must exist to define the CIM classname(es) .
# for which this provider is responsible.
#: provider_classnames (:term:`string` or list of :term:`string`):
#: The classname for this provider
provider_classnames = FILTER_CLASSNAME
def __init__(self, cimrepository):
"""
Parameters:
cimrepository (:class:`~pywbem_mock.BaseRepository` or subclass):
Defines the CIM repository to be used by the provider.
"""
super(CIMIndicationFilterProvider, self).__init__(cimrepository)
if not self.find_interop_namespace():
raise CIMError(
CIM_ERR_INVALID_PARAMETER,
_format("Cannot create indication filter provider for class: "
"{0}. "
"No Interop namespace exists in the CIM repository. "
"Valid Interop namespaces are: {1}",
FILTER_CLASSNAME,
", ".join(self.interop_namespace_names)))
self.installed = False # test if provider previously installed.
self.conn = None
def __repr__(self):
return _format(
"CIMIndicationFilterProvider("
"provider_type={s.provider_type}, "
"provider_classnames={s.provider_classnames})",
s=self)
def CreateInstance(self, namespace, new_instance):
# pylint: disable=invalid-name
"""
Create an instance of the CIM_IndicationFilter class in an Interop
namespace of the CIM repository, and if not yet existing create the new
namespace in the CIM repository.
See `~pywbem_mock.InstanceWriteProvider.CreateInstance` for
documentation of validation and description of input parameters, noting
extra conditions for this provider as described below:
Parameters:
namespace (:term:`string`):
Must be a valid Interop namespace.
new_instance (:class:`~pywbem.CIMInstance`):
The following applies regarding its properties:
* 'Name' property: This property is required since it defines the
name of the new namespace to be created.
* 'CreationClassName' property: This property is required and its
value must match the class name of the new instance.
Raises:
:exc:`~pywbem.CIMError`: (CIM_ERR_INVALID_PARAMETER) if namespace
is not the Interop namespace in the CIM repository or the Name
property does not exist or the other properties cannot be added to
the instance.
"""
self.parameter_is_interop(namespace, new_instance.classname)
required_properties = ['Name']
self.validate_required_properties_exist(new_instance, namespace,
required_properties)
# Validates and possibly modifies the key properties except Name
self.fix_key_properties(new_instance)
# Note: No test for SourceNamespace valid namespaces because profile
# allows creating filters for not-yet-created namespaces
# Add missing properties that the might come from CIM_IndicationService
# Issue # 2719, Should the following be set by the server or client
set_property(new_instance, 'IndividualSubscriptionSupported', True)
set_property(new_instance, 'SourceNamespace',
CIMProperty('SourceNamespace', None, type='string'))
set_property(new_instance, 'Description',
"Pywbem mock CIMIndicationFilterProvider instance")
# Create the CIM instance for the new namespace in the CIM repository,
# by delegating to the default provider method.
return super(CIMIndicationFilterProvider, self).CreateInstance(
namespace, new_instance)
def ModifyInstance(self, modified_instance, IncludeQualifiers=None):
# pylint: disable=invalid-name
"""
Modification of CIM_IndicationFilter instance allowed for selected
properties. See the documentation in
CommonMethodsMixin.validate_modify_instance for parameter
documentation.
"""
modifiable_properties = ['IndividualSubscriptionSupported']
self.validate_modify_instance(
modified_instance,
modifiable_properties=modifiable_properties,
IncludeQualifiers=IncludeQualifiers)
return super(CIMIndicationFilterProvider, self).ModifyInstance(
modified_instance, IncludeQualifiers=IncludeQualifiers)
def DeleteInstance(self, InstanceName): # pylint: disable=invalid-name
"""
Delete an instance of the CIM_IndicationFilter class in an Interop
namespace of the CIM repository unless it has an outstanding
association through a subscription.
See `~pywbem_mock.InstanceWriteProvider.CreateInstance` for
documentation of validation and description of input parameters, noting
extra conditions for this provider as described below:
The namespace to be deleted must be empty and must not be the Interop
namespace.
Parameters:
InstanceName: (:class:`~pywbem.CIMInstanceName`):
The keybinding `Name` must exist; it defines the namespace to be
deleted.
Raises:
:exc:`~pywbem.CIMError`: (CIM_ERR_INVALID_PARAMETER)
:exc:`~pywbem.CIMError`: (CIM_ERR_NAMESPACE_NOT_EMPTY)
"""
# The provider dispatcher ensures that provider methods are only called
# for the registered classes.
# And this provider sets only a single class, not a list.
assert InstanceName.classname.lower() == \
CIMIndicationFilterProvider.provider_classnames.lower()
self.validate_no_subscription(InstanceName)
# Delete the instance from the CIM repository
instance_store = self.cimrepository.get_instance_store(
InstanceName.namespace)
instance_store.delete(InstanceName)
class CIMListenerDestinationProvider(CommonMethodsMixin, InstanceWriteProvider):
# pylint: disable=line-too-long
"""
Implements the user defined provider for the class
CIM_ListenerDestinationCIMXML.
This provider provides the create, modify, and delete methods for adding an
instance of the class CIM_ListenerDestinationCIMXML when a namespace is
created or deleted in a pywbem mock environment.
This class and the instances of this class only exist in the WBEM server
Interop namespace per DMTF definitions.
This class __init__ saves the cimrepository variable used by methods in
this class and by methods of its superclasses (i.e. InstanceWriteProvider).
The provider defines the class level attribute `provider_classnames`
(CIM_ListenerDestinationCIMXML)
This provider presumes that an Interop namespace has been created before
the provider object is constructed and fails the constructor if
there is not interop_namespace
""" # noqa: E501
# pylint: enable=line-too-long
# This class level attribute must exist to define the CIM classname(es) .
# for which this provider is responsible.
#: provider_classnames (:term:`string`):
#: The classnames for this provider
provider_classnames = LISTENERDESTINATION_CLASSNAME
def __init__(self, cimrepository):
"""
Parameters:
cimrepository (:class:`~pywbem_mock.BaseRepository` or subclass):
Defines the CIM repository to be used by the provider.
"""
super(CIMListenerDestinationProvider, self).__init__(cimrepository)
if not self.find_interop_namespace():
raise CIMError(
CIM_ERR_INVALID_PARAMETER,
_format("Cannot create listener destination provider for "
"classes: ({0}). "
"No Interop namespace exists in the CIM repository. "
"Valid Interop namespaces are: {1}",
", ".join(LISTENERDESTINATION_CLASSNAME),
", ".join(self.interop_namespace_names)))
self.installed = False # test if provider previously installed.
self.conn = None
def __repr__(self):
return _format(
"CIMListenerDestinationProvider("
"provider_type={s.provider_type}, "
"provider_classnames={s.provider_classnames})",
s=self)
def CreateInstance(self, namespace, new_instance):
# pylint: disable=invalid-name
"""
Create an instance of the CIM_ListenerDestinationCIMXML class in an
Interop namespace of the CIM repository, and if not yet existing create
the new namespace in the CIM repository.
See `~pywbem_mock.InstanceWriteProvider.CreateInstance` for
documentation of validation and description of input parameters, noting
extra conditions for this provider as described below:
Parameters:
namespace (:term:`string`):
Must be a valid Interop namespace.
new_instance (:class:`~pywbem.CIMInstance`):
The following applies regarding its properties:
* 'Name' property: This property is required since it defines the
name of the new namespace to be created.
* 'CreationClassName' property: This property is required and its
value must match the class name of the new instance.
Raises:
:exc:`~pywbem.CIMError`: (CIM_ERR_INVALID_PARAMETER) if namespace
is not the Interop namespace in the CIM repository or the Name
property does not exist or the other properties cannot be added to
the instance.
"""
self.parameter_is_interop(namespace, new_instance.classname)
ccn_pname = 'CreationClassName'
name_pname = 'Name'
sys_pname = 'SystemName'
sccn_pname = 'SystemCreationClassName'
required_properties = [name_pname]
# Validate that required properties are specified in the new instance
self.validate_required_properties_exist(new_instance, namespace,
required_properties)
# Validate or fix other key values in the new instance
def _fix_key_prop(pname, new_prop_value, replacement=None):
if pname not in new_instance or \
new_instance[pname].lower != new_prop_value.lower():
new_instance[pname] = replacement or new_prop_value
# Set the keys to default if they don't exist or have invalid value
_fix_key_prop(ccn_pname, new_instance.classname,
new_instance.classname)
_fix_key_prop(sys_pname, SYSTEMNAME)
_fix_key_prop(sccn_pname, SYSTEMCREATIONCLASSNAME)
# Add missing properties that the might come from CIM_IndicationService
# if not already in instance
set_property(new_instance, 'Protocol', Uint16(2))
set_property(new_instance, 'PersistenceType', Uint16(2))
set_property(new_instance, 'Description',
"pywbem mock CIMListenerDestinationProvider instance")
# Create the CIM instance for the new namespace in the CIM repository,
# by delegating to the default provider method.
return super(CIMListenerDestinationProvider, self).CreateInstance(
namespace, new_instance)
def ModifyInstance(self, modified_instance, IncludeQualifiers=None):
# pylint: disable=invalid-name
"""
Modification of CIM_ListenerDestinationXIMXML instance allowed only for
selected properties. See the documentation in
CommonMethodsMixin.validate_modify_instance for parameter documentation.
"""
modifiable_properties = []
self.validate_modify_instance(
modified_instance,
modifiable_properties=modifiable_properties,
IncludeQualifiers=IncludeQualifiers)
return super(CIMListenerDestinationProvider, self).ModifyInstance(
modified_instance, IncludeQualifiers=IncludeQualifiers)
def DeleteInstance(self, InstanceName): # pylint: disable=invalid-name
"""
Delete an instance of the CIM_ListenerDestinationCIMXML class in an
Interop namespace of the CIM repository unless it has an outstanding
association through a subscription.
See `~pywbem_mock.InstanceWriteProvider.CreateInstance` for
documentation of validation and description of input parameters, noting
extra conditions for this provider as described below:
The namespace to be deleted must be empty and must not be the Interop
namespace.
Parameters:
InstanceName: (:class:`~pywbem.CIMInstanceName`):
The keybinding `Name` must exist; it defines the namespace to be
deleted.
Raises:
:exc:`~pywbem.CIMError`: (CIM_ERR_INVALID_PARAMETER)
:exc:`~pywbem.CIMError`: (CIM_ERR_NAMESPACE_NOT_EMPTY)
"""
# The provider dispatcher ensures that provider methods are only called
# for the registered classes.
# And this provider sets a list of classes as provider classes
assert InstanceName.classname in \
CIMListenerDestinationProvider.provider_classnames
self.validate_no_subscription(InstanceName)
return super(CIMListenerDestinationProvider, self).DeleteInstance(
InstanceName)
class CIMIndicationSubscriptionProvider(CommonMethodsMixin,
InstanceWriteProvider):
# pylint: disable=line-too-long
"""
Implements the user defined provider for the class CIM_IndicationFilter.
This provider provides the create, modify, and delete methods for
adding an instance of the class CIM_Namspace when a namespace is created or
deleted in a pywbem mock environment.
This class and the instances of this class only exist in the WBEM server
Interop namespace per DMTF definitions.
This class __init__ saves the cimrepository variable used by methods in
this class and by methods of its superclasses (i.e. InstanceWriteProvider).
The provider defines the class level attribute `provider_classnames`
(CIM_indicationsubscription)
This provider assures that an Interop namespace has been created before
the provider object is constructed and fails the constructor if
there is not interop_namespace
""" # noqa: E501
# pylint: enable=line-too-long
# This class level attribute must exist to define the CIM classname(es) .
# for which this provider is responsible.
#: provider_classnames (:term:`string`):
#: The classname for this provider
provider_classnames = SUBSCRIPTION_CLASSNAME
def __init__(self, cimrepository):
"""
Parameters:
cimrepository (:class:`~pywbem_mock.BaseRepository` or subclass):
Defines the CIM repository to be used by the provider.
"""
super(CIMIndicationSubscriptionProvider, self).__init__(cimrepository)
if not self.find_interop_namespace():
raise CIMError(
CIM_ERR_INVALID_PARAMETER,
_format("Cannot create indication subscription provider for "
"class: {0}. "
"No Interop namespace exists in the CIM repository. "
"Valid Interop namespaces are: {1}",
FILTER_CLASSNAME,
", ".join(self.interop_namespace_names)))
self.installed = False # test if provider previously installed.
self.conn = None
def __repr__(self):
return _format(
"CIMIndicationFilterProvider("
"provider_type={s.provider_type}, "
"provider_classnames={s.provider_classnames})",
s=self)
def CreateInstance(self, namespace, new_instance):
# pylint: disable=invalid-name
"""
Create an instance of the CIM_IndicationFilter class in an Interop
namespace of the CIM repository, and if not yet existing create the new
namespace in the CIM repository.
See `~pywbem_mock.InstanceWriteProvider.CreateInstance` for
documentation of validation and description of input parameters, noting
extra conditions for this provider as described below:
Parameters:
namespace (:term:`string`):
Must be a valid Interop namespace.
new_instance (:class:`~pywbem.CIMInstance`):
The following applies regarding its properties:
* The 'Filter' and 'Handler' reference properties must exist.
* If 'SubscriptionDuration' exists, 'SubscriptionTimeRemaining'
will be set.
* 'CreationClassName' property: This property is required and its
value must match the class name of the new instance.
Raises:
:exc:`~pywbem.CIMError`: (CIM_ERR_INVALID_PARAMETER) if namespace
is not the Interop namespace in the CIM repository or the Name
property does not exist or the other properties cannot be added to
the instance.
"""
self.parameter_is_interop(namespace, new_instance.classname)
required_properties = ["Filter", "Handler"]
self.validate_required_properties_exist(new_instance, namespace,
required_properties)
# Add missing properties that the might come from CIM_IndicationService
new_instance['SubscriptionStartTime'] = CIMDateTime.now()
new_instance['TimeOfLastStateChange'] = CIMDateTime.now()
# Conditionally add the following properties
set_property(new_instance, 'OnFatalErrorPolicy', Uint16(2))
set_property(new_instance, 'RepeatNotificationPolicy', Uint16(2))
set_property(new_instance, 'SubscriptionState', Uint16(2))
if 'SubscriptionDuration' in new_instance:
new_instance['SubscriptionTimeRemaining'] = \
new_instance['SubscriptionDuration']
else:
new_instance['SubscriptionDuration'] = CIMProperty(
'SubscriptionDuration', None, type='uint64')
set_property(new_instance, 'SubscriptionInfo',
CIMProperty('SubscriptionInfo', None, type='string'))
set_property(new_instance, 'Description',
"Pywbem mock CIMIndicationSubscriptionProvider instance")
# Create the CIM instance for the new namespace in the CIM repository,
# by delegating to the default provider method.
return super(CIMIndicationSubscriptionProvider, self).CreateInstance(
namespace, new_instance)
def ModifyInstance(self, modified_instance, IncludeQualifiers=None):
# pylint: disable=invalid-name
"""
Modification of CIM_IndicationSubscription instance allowed only for
selected properties. See the documentation in
CommonMethodsMixin.validate_modify_instance for parameter documentation.
"""
# NOTE: The choice of modifiable properties is just to support tests
# and may not reflect user needs since profile definition is
# flexible
modifiable_properties = ['SubscriptionInfo', 'SubscriptionState',
'SubscriptionDuration']
# Validates the modify instance but does not change any properties.
# If not valid, it generates exception
self.validate_modify_instance(
modified_instance, modifiable_properties=modifiable_properties,
IncludeQualifiers=IncludeQualifiers)
if modified_instance['SubscriptionDuration']:
modified_instance['SubscriptionTimeRemaining'] = \
modified_instance['SubscriptionDuration']
if modified_instance['SubscriptionDuration']:
modified_instance['SubscriptionTimeRemaining'] = \
modified_instance['SubscriptionDuration']
modified_instance['TimeOfLastStateChange'] = CIMDateTime.now()
return super(CIMIndicationSubscriptionProvider, self).ModifyInstance(
modified_instance, IncludeQualifiers=IncludeQualifiers)
def DeleteInstance(self, InstanceName): # pylint: disable=invalid-name
"""
Delete an instance of the CIM_IndicationSubscription class in an Interop
namespace of the CIM repository unless it has an outstanding
association through a subscription.
See `~pywbem_mock.InstanceWriteProvider.CreateInstance` for
documentation of validation and description of input parameters, noting
extra conditions for this provider as described below:
The namespace to be deleted must be empty and must not be the Interop
namespace.
Parameters:
InstanceName: (:class:`~pywbem.CIMInstanceName`):
The keybinding `Name` must exist; it defines the namespace to be
deleted.
Raises:
:exc:`~pywbem.CIMError`: (CIM_ERR_INVALID_PARAMETER)
:exc:`~pywbem.CIMError`: (CIM_ERR_NAMESPACE_NOT_EMPTY)
"""
# The provider dispatcher ensures that provider methods are only called
# for the registered classes.
# And this provider sets only a single class, not a list.
assert InstanceName.classname.lower() == \
CIMIndicationSubscriptionProvider.provider_classnames.lower()
return super(CIMIndicationSubscriptionProvider, self).DeleteInstance(
InstanceName)