File: base.py

package info (click to toggle)
python-sushy 5.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,620 kB
  • sloc: python: 14,026; makefile: 24; sh: 2
file content (883 lines) | stat: -rw-r--r-- 31,234 bytes parent folder | download | duplicates (2)
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
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
# Copyright 2017 Red Hat, Inc.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import abc
import collections
import copy
import enum
from importlib import resources
import io
import json
import logging
import zipfile

from sushy import exceptions
from sushy.resources import constants
from sushy.resources import oem
from sushy import utils


LOG = logging.getLogger(__name__)


class Field:
    """Definition for fields fetched from JSON."""

    def __init__(self, path, required=False, default=None,
                 adapter=lambda x: x):
        """Create a field definition.

        :param path: JSON field to fetch the value from. Either a string,
            or a list of strings in case of a nested field.
        :param required: whether this field is required. Missing required,
            but not defaulted, fields result in MissingAttributeError.
        :param default: the default value to use when the field is missing.
        :param adapter: a function to call to transform and/or validate
            the received value. UnicodeError, ValueError or TypeError from
            this call are reraised as MalformedAttributeError.
        """
        if not callable(adapter):
            raise TypeError("Adapter must be callable")

        if not isinstance(path, list):
            path = [path]

        elif not path:
            raise ValueError('Path cannot be empty')

        self._path = path
        self._required = required
        self._default = default
        self._adapter = adapter

    def _get_item(self, dct, key_or_callable, **context):
        if not callable(key_or_callable):
            return dct[key_or_callable]

        for candidate_key in dct:
            if key_or_callable(
                    candidate_key, value=dct[candidate_key], **context):
                return dct[candidate_key]

        raise KeyError(key_or_callable)

    def _load(self, body, resource, nested_in=None):
        """Load this field from a JSON object.

        :param body: parsed JSON body.
        :param resource: ResourceBase instance for which the field is loaded.
        :param nested_in: parent resource path (for error reporting only),
            must be a list of strings or None.
        :raises: MissingAttributeError if a required field is missing
            and not defaulted.
        :raises: MalformedAttributeError on invalid field value or type.
        :returns: loaded and verified value
        """
        name = self._path[-1]
        for path_item in self._path[:-1]:
            body = body.get(path_item, {})

        try:
            item = self._get_item(body, name)

        except KeyError:
            if self._required:
                path = (nested_in or []) + self._path

                if self._default is None:
                    raise exceptions.MissingAttributeError(
                        attribute='/'.join(path),
                        resource=resource.path)

                logging.warning(
                    'Applying default "%s" on required, but missing '
                    'attribute "%s"', self._default, path)

            # Do not run the adapter on the default value
            return self._default

        # NOTE(etingof): this is just to account for schema violation
        if item is None:
            return

        try:
            return self._adapter(item)

        except (UnicodeError, ValueError, TypeError) as exc:
            path = (nested_in or []) + self._path
            raise exceptions.MalformedAttributeError(
                attribute='/'.join(path),
                resource=resource.path,
                error=exc)


def _collect_fields(resource):
    """Collect fields from the JSON.

    :param resource: ResourceBase or CompositeField instance.
    :returns: generator of tuples (key, field)
    """
    for attr in dir(resource.__class__):
        field = getattr(resource.__class__, attr)
        if isinstance(field, Field):
            yield (attr, field)


class CompositeField(collections.abc.Mapping, Field, metaclass=abc.ABCMeta):
    """Base class for fields consisting of several sub-fields."""

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._subfields = dict(_collect_fields(self))

    def _load(self, body, resource, nested_in=None):
        """Load the composite field.

        :param body: parent JSON body.
        :param resource: parent resource.
        :param nested_in: parent resource name (for error reporting only).
        :returns: a new object with sub-fields attached to it.
        """
        nested_in = (nested_in or []) + self._path
        value = super()._load(body, resource)
        if value is None:
            return None

        # We need a new instance, as this method is called a singleton instance
        # that is attached to a class (not instance) of a resource or another
        # CompositeField. We don't want to end up modifying this instance.
        instance = copy.copy(self)
        for attr, field in self._subfields.items():
            # Hide the Field object behind the real value
            setattr(instance, attr, field._load(value, resource, nested_in))

        return instance

    # Satisfy the mapping interface, see
    # https://docs.python.org/3/library/collections.abc.html#collections.abc.Mapping

    def __getitem__(self, key):
        if key in self._subfields:
            return getattr(self, key)
        else:
            raise KeyError(key)

    def __len__(self):
        return len(self._subfields)

    def __iter__(self):
        return iter(self._subfields)


class ListField(Field):
    """Base class for fields consisting of a list of several sub-fields."""

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._subfields = dict(_collect_fields(self))

    def _load(self, body, resource, nested_in=None):
        """Load the field list.

        :param body: parent JSON body.
        :param resource: parent resource.
        :param nested_in: parent resource name (for error reporting only).
        :returns: a new list object containing subfields.
        """
        nested_in = (nested_in or []) + self._path
        values = super()._load(body, resource)
        if values is None:
            return None

        # Initialize the list that will contain each field instance
        instances = []
        for value in values:
            instance = copy.copy(self)
            for attr, field in self._subfields.items():
                # Hide the Field object behind the real value
                setattr(instance, attr, field._load(value,
                                                    resource,
                                                    nested_in))
            instances.append(instance)

        return instances

    def __getitem__(self, key):
        return getattr(self, key)


class LinksField(CompositeField):
    """Reference to linked resources."""
    oem_vendors = Field('Oem', adapter=list)


class DictionaryField(Field):
    """Base class for fields consisting of dictionary of several sub-fields."""

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._subfields = dict(_collect_fields(self))

    def _load(self, body, resource, nested_in=None):
        """Load the dictionary.

        :param body: parent JSON body.
        :param resource: parent resource.
        :param nested_in: parent resource name (for error reporting only).
        :returns: a new dictionary object containing subfields.
        """
        nested_in = (nested_in or []) + self._path
        values = super()._load(body, resource)
        if values is None:
            return None

        instances = {}
        for key, value in values.items():
            instance_value = copy.copy(self)
            for attr, field in self._subfields.items():
                # Hide the Field object behind the real value
                setattr(instance_value, attr, field._load(value,
                                                          resource,
                                                          nested_in))
            instances[key] = instance_value

        return instances

    def __getitem__(self, key):
        return getattr(self, key)


class MappedField(Field):
    """Field taking real value from a mapping."""

    def __init__(self, field, mapping, required=False, default=None):
        """Create a mapped field definition.

        :param field: JSON field to fetch the value from. This can be either
            a string or a list of string. In the latter case, the value will
            be fetched from a nested object.
        :param mapping: a mapping to take values from, a dictionary or
            an enumeration.
        :param required: whether this field is required. Missing required,
            but not defaulted, fields result in MissingAttributeError.
        :param default: the default value to use when the field is missing.
            This value is not matched against the mapping.
        """
        if isinstance(mapping, type) and issubclass(mapping, enum.Enum):
            def adapter(value):
                try:
                    return mapping(value)
                except ValueError:
                    return None

        elif isinstance(mapping, collections.abc.Mapping):
            adapter = mapping.get
        else:
            raise TypeError("The mapping argument must be a mapping or "
                            "an enumeration")

        super().__init__(
            field, required=required, default=default, adapter=adapter)


class MappedListField(Field):
    """Field taking a list of values with a mapping for the values

    Given JSON {'field':['xxx', 'yyy']}, a sushy resource definition and
    mapping {'xxx':'a', 'yyy':'b'}, the sushy object to come out will be like
    resource.field = ['a', 'b']
    """

    def __init__(self, field, mapping, required=False, default=None):
        """Create a mapped list field definition.

        :param field: JSON field to fetch the list of values from.
        :param mapping: a mapping for the list elements.
        :param required: whether this field is required. Missing required,
            but not defaulted, fields result in MissingAttributeError.
        :param default: the default value to use when the field is missing.
        """
        if isinstance(mapping, type) and issubclass(mapping, enum.Enum):
            def adapter(value):
                try:
                    return mapping(value)
                except ValueError:
                    return None

        elif isinstance(mapping, collections.abc.Mapping):
            adapter = mapping.get
        else:
            raise TypeError("The mapping argument must be a mapping or "
                            "an enumeration")

        self._mapping_adapter = adapter
        super().__init__(
            field, required=required, default=default,
            adapter=lambda x: x)

    def _load(self, body, resource, nested_in=None):
        """Load the mapped list.

        :param body: parent JSON body.
        :param resource: parent resource.
        :param nested_in: parent resource name (for error reporting only).
        :returns: a new list object containing the mapped values.
        """
        nested_in = (nested_in or []) + self._path
        values = super()._load(body, resource)

        if values is None:
            return

        instances = [self._mapping_adapter(value) for value in values
                     if self._mapping_adapter(value) is not None]

        return instances


class MessageListField(ListField):
    """List of messages with details of settings update status"""

    message_id = Field('MessageId')
    """The key for this message which can be used
    to look up the message in a message registry
    """

    message = Field('Message')
    """Human readable message, if provided"""

    severity = MappedField('Severity', constants.Severity)
    """Severity of the error"""

    resolution = Field('Resolution')
    """Used to provide suggestions on how to resolve
    the situation that caused the error
    """

    _related_properties = Field('RelatedProperties')
    """List of properties described by the message"""

    message_args = Field('MessageArgs')
    """List of message substitution arguments for the message
    referenced by `message_id` from the message registry
    """


class FieldData:
    """Contains data to be used when constructing Fields"""

    def __init__(self, status_code, headers, json_doc):
        """Initializes the FieldData instance"""
        self._status_code = status_code
        self._headers = headers
        self._json_doc = json_doc

    @property
    def status_code(self):
        """The status code"""
        return self._status_code

    @property
    def headers(self):
        """The headers"""
        return self._headers

    @property
    def json_doc(self):
        """The parsed JSON body"""
        return self._json_doc


class AbstractDataReader(metaclass=abc.ABCMeta):

    def set_connection(self, connector, path):
        """Sets mandatory connection parameters

        :param connector: A Connector instance
        :param path: path of the resource
        """
        self._conn = connector
        self._path = path

    @abc.abstractmethod
    def get_data(self):
        """Based on data source get data and parse to JSON"""


class JsonDataReader(AbstractDataReader):
    """Gets the data from HTTP response given by path"""

    def get_data(self):
        """Gets JSON file from URI directly"""
        data = self._conn.get(path=self._path)
        try:
            json_data = data.json() if data.content else {}
        except Exception as exc:
            LOG.error("Unable to parse JSON in response. %(exc)s. The server "
                      "returned:\n%(data)s",
                      {'exc': exc, 'data': data.content})
            raise
        return FieldData(data.status_code, data.headers, json_data)


class JsonPublicFileReader(AbstractDataReader):
    """Loads the data from the Internet"""

    def get_data(self):
        """Get JSON file from full URI"""
        data = self._conn.get(self._path)

        return FieldData(data.status_code, data.headers, data.json())


class JsonArchiveReader(AbstractDataReader):
    """Gets the data from JSON file in archive"""

    def __init__(self, archive_file):
        """Initializes the reader

        :param archive_file: file name of JSON file in archive
        """
        self._archive_file = archive_file

    def get_data(self):
        """Gets JSON file from archive. Currently supporting ZIP only"""

        data = self._conn.get(path=self._path)
        if data.headers.get('content-type') == 'application/zip':
            try:
                archive = zipfile.ZipFile(io.BytesIO(data.content))
                json_data = json.loads(archive.read(self._archive_file)
                                       .decode(encoding='utf-8'))
                return FieldData(data.status_code, data.headers, json_data)
            except (zipfile.BadZipfile, ValueError) as e:
                raise exceptions.ArchiveParsingError(
                    path=self._path, error=e)
        else:
            LOG.error('Support for %(type)s not implemented',
                      {'type': data.headers['content-type']})

            return FieldData(data.status_code, data.headers, None)


class JsonPackagedFileReader(AbstractDataReader):
    """Gets the data from packaged file given by path"""

    def __init__(self, resource_package_name):
        """Initializes the reader

        :param resource_package: Python package/module name
        """
        self._resource_package_name = resource_package_name

    def get_data(self):
        """Gets JSON file from packaged file denoted by path"""

        ref = resources.files(self._resource_package_name).joinpath(self._path)
        with ref.open(encoding='utf-8') as fp:
            json_data = json.load(fp)
            return FieldData(None, None, json_data)


def get_reader(connector, path, reader=None):
    """Create and configure the reader.

    :param connector: A Connector instance
    :param path: sub-URI path to the resource.
    :param reader: Reader to use to fetch JSON data.
    :returns: the reader
    """
    if reader is None:
        reader = JsonDataReader()
    reader.set_connection(connector, path)

    return reader


class ResourceBase(metaclass=abc.ABCMeta):

    redfish_version = None
    """The Redfish version"""

    _oem_vendors = Field('Oem', adapter=list)
    """The list of OEM extension names for this resource."""

    links = LinksField('Links')

    _log_resource_body = True
    """Whether to log the whole resource body in debug mode."""

    def __init__(self,
                 connector,
                 path='',
                 redfish_version=None,
                 registries=None,
                 reader=None,
                 json_doc=None,
                 root=None):
        """A class representing the base of any Redfish resource

        Invokes the ``refresh()`` method of resource for the first
        time from here (constructor).
        :param connector: A Connector instance
        :param path: sub-URI path to the resource.
        :param redfish_version: The version of Redfish. Used to construct
            the object according to schema of the given version.
        :param registries: Dict of Redfish Message Registry objects to be
            used in any resource that needs registries to parse messages
        :param reader: Reader to use to fetch JSON data.
        :param json_doc: parsed JSON document in form of Python types.
        :param root: Sushy root object. Empty for Sushy root itself.
        """
        self._conn = connector
        self._path = path
        self._json = None
        self.redfish_version = redfish_version
        self._registries = registries
        # Note(deray): Indicates if the resource holds stale data or not.
        # Starting off with True and eventually gets set to False when
        # attribute values are fetched.
        self._is_stale = True

        self._reader = get_reader(connector, path, reader)
        self._root = root
        self.refresh(json_doc=json_doc)

    def _get_value(self, val):
        """Iterate through the input to get values for all attributes

        :param val: Either a value or a resource
        :returns: Attribute value, which may be a dictionary
        """
        if isinstance(val, dict):
            subfields = {}
            for key, s_val in val.items():
                subfields[key] = self._get_value(s_val)
            return subfields

        elif isinstance(val, list):
            return [self._get_value(val[i]) for i in range(len(val))]

        elif (isinstance(val, DictionaryField)
              or isinstance(val, CompositeField)
              or isinstance(val, ListField)):
            subfields = {}
            for attr, field in val._subfields.items():
                subfields[attr] = self._get_value(val.__getitem__(attr))
            return subfields

        return val

    def _get_registry(self, identity, language='en', description='registry'):
        """Get a registry with the given identity.

        :param identity: The registry identity.
        :param language: RFC 5646 language code for Message Registries.
            Indicates language of registry to be used. Defaults to 'en'.
        :param description: Human-readable description to use in logging.
        :returns: the corresponding registry object or None.
        """
        registries = self._registries
        if not registries:
            LOG.info('No %s is available', description)
            return None

        for key, registry in registries.items():
            if (registry
                    and identity in (key, registry.identity)):
                # NOTE(iurygregory): some registries may have "en-US"
                # as their language, in this case we can check if the
                # registry language starts with the requested language.
                registry_language = registry.language.lower().split('-', 1)[0]
                if (language != registry.language
                        and language.lower() != registry_language):
                    LOG.debug('Found %(descr)s but its language %(reg_lang)s '
                              'does not match the requested %(lang)s',
                              {'descr': description,
                               'lang': language,
                               'reg_lang': registry.language})
                    continue

                return registry

        avail = ', '.join(f'{reg.identity} ({reg.language})'
                          for reg in registries.values())
        LOG.info('%(descr)s %(registry)s not available for language %(lang)s; '
                 'available are: %(avail)s',
                 {'descr': description,
                  'registry': self._attribute_registry,
                  'lang': language,
                  'avail': avail})
        return None

    def _parse_attributes(self, json_doc):
        """Parse the attributes of a resource.

        Parsed JSON fields are set to `self` as declared in the class.

        :param json_doc: parsed JSON document in form of Python types
        :returns: dictionary of attribute/values after parsing
        """
        settings = {}
        for attr, field in _collect_fields(self):
            # Hide the Field object behind the real value
            setattr(self, attr, field._load(json_doc, self))

            # Get the attribute/value pairs that have been parsed
            settings[attr] = self._get_value(getattr(self, attr))

        return settings

    def _get_etag(self):
        """Returns the ETag of the HTTP request if any was specified.

        :returns ETag or None
        """
        return self._get_headers().get('ETag')

    def _get_headers(self):
        """Returns the HTTP headers of the request for the resource.

        :returns: dict of HTTP headers
        """
        return self._reader.get_data()._headers

    def _allow_patch(self):
        """Returns if the resource supports the PATCH HTTP method.

        If the resource supports the PATCH HTTP method for updates,
        it will return it in the Allow HTTP header.
        :returns: Boolean flag if PATCH is supported or not
        """
        allow_header = self._get_headers().get('Allow', '')
        methods = set([h.strip().upper() for h in allow_header.split(',')])
        return "PATCH" in methods

    def refresh(self, force=True, json_doc=None):
        """Refresh the resource

        Freshly retrieves/fetches the resource attributes and invokes
        ``_parse_attributes()`` method on successful retrieval.
        It is recommended not to override this method in concrete ResourceBase
        classes. Resource classes can place their refresh specific operations
        in ``_do_refresh()`` method, if needed. This method represents the
        template method in the paradigm of Template design pattern.

        :param force: if set to False, will only refresh if the resource is
            marked as stale, otherwise neither it nor its subresources will
            be refreshed.
        :param json_doc: parsed JSON document in form of Python types.
        :raises: ResourceNotFoundError
        :raises: ConnectionError
        :raises: HTTPError
        """
        # Note(deray): Don't re-fetch / invalidate the sub-resources if the
        # resource is "_not_ stale" (i.e. fresh) OR _not_ forced.
        if not self._is_stale and not force:
            return

        if json_doc:
            self._json = json_doc
        else:
            self._json = self._reader.get_data().json_doc

        attributes = self._parse_attributes(self._json)
        LOG.debug('Received representation of %(type)s %(path)s: %(json)s',
                  {'type': self.__class__.__name__,
                   'path': self._path,
                   'json': (attributes if self._log_resource_body
                            else '<stripped>')})
        self._do_refresh(force)

        # Mark it fresh
        self._is_stale = False

    def _do_refresh(self, force):
        """Primitive method to be overridden by refresh related activities.

        Derived classes are supposed to override this method with the
        resource specific refresh operations to be performed. This is a
        primitive method in the paradigm of Template design pattern.

        As for the base implementation of this method the approach taken is:
        On refresh, all sub-resources are marked as stale. That means
        invalidate (or undefine) the exposed attributes for nested resources
        for fresh evaluation in subsequent calls to those exposed attributes.
        In other words greedy-refresh is not done for them, unless forced by
        ``force`` argument.

        :param force: should force refresh the resource and its sub-resources,
            if set to True.
        :raises: ResourceNotFoundError
        :raises: ConnectionError
        :raises: HTTPError
        """
        utils.cache_clear(self, force_refresh=force)

    def invalidate(self, force_refresh=False):
        """Mark the resource as stale, prompting refresh() before getting used.

        If ``force_refresh`` is set to True, then it invokes ``refresh()``
        on the resource.

        :param force_refresh: will invoke refresh on the resource,
            if set to True.
        :raises: ResourceNotFoundError
        :raises: ConnectionError
        :raises: HTTPError
        """
        self._is_stale = True
        if force_refresh:
            self.refresh()

    @property
    def oem_vendors(self):
        return list(
            set((self._oem_vendors or []) + (self.links.oem_vendors or []))
        )

    @property
    def json(self):
        return self._json

    @property
    def path(self):
        return self._path

    def clone_resource(self, new_resource, path=''):
        """Instantiate given resource using existing BMC connection context"""
        return new_resource(
            self._conn, path or self.path,
            redfish_version=self.redfish_version,
            reader=self._reader,
            root=self.root)

    @property
    def resource_name(self):
        return utils.camelcase_to_underscore_joined(self.__class__.__name__)

    def get_oem_extension(self, vendor):
        """Get the OEM extension instance for this resource by OEM vendor

        :param vendor: the OEM vendor string which is the vendor-specific
            extensibility identifier. Examples are 'Contoso', 'Hpe'.
            Possible value can be got from ``oem_vendors`` attribute.
        :returns: the Redfish resource OEM extension instance.
        :raises: OEMExtensionNotFoundError
        """
        return oem.get_resource_extension_by_vendor(
            self.resource_name, vendor, self)

    @property
    def registries(self):
        return self._registries

    @property
    def root(self):
        return self._root


class ResourceLinksBase(ResourceBase, metaclass=abc.ABCMeta):

    def __init__(self, connector, path, redfish_version=None, registries=None,
                 root=None):
        """A class representing the base of any Redfish resource collection

        It gets inherited from ``ResourceBase`` and invokes the base class
        constructor.
        :param connector: A Connector instance
        :param path: sub-URI path to the resource collection.
        :param redfish_version: The version of Redfish. Used to construct
            the object according to schema of the given version.
        :param registries: Dict of Redfish Message Registry objects to be
            used in any resource that needs registries to parse messages.
        :param root: Sushy root object. Empty for Sushy root itself.
        """
        super().__init__(connector, path, redfish_version, registries,
                         root=root)
        LOG.debug('Received %(count)d member(s) for %(type)s %(path)s',
                  {'count': len(self.members_identities),
                   'type': self.__class__.__name__, 'path': self._path})

    @property
    @abc.abstractmethod
    def members_identities(self):
        """A sequence with members identities"""

    @property
    @abc.abstractmethod
    def _resource_type(self):
        """The resource class that the collection contains.

        Override this property to specify the resource class that the
        collection contains.
        """

    def get_member(self, identity):
        """Given the identity return a ``_resource_type`` object

        :param identity: The identity of the ``_resource_type``
        :returns: The ``_resource_type`` object
        :raises: ResourceNotFoundError
        """
        return self._resource_type(
            self._conn, identity, redfish_version=self.redfish_version,
            registries=self.registries,
            root=self.root)

    @utils.cache_it
    def get_members(self):
        """Return a list of ``_resource_type`` objects present in collection

        :returns: A list of ``_resource_type`` objects
        """
        return [self.get_member(id_) for id_ in self.members_identities]


class ResourceCollectionBase(ResourceLinksBase):

    name = Field('Name')
    """The name of the collection"""

    members_identities = Field('Members', default=[],
                               adapter=utils.get_members_identities)
    """A tuple with the members identities"""


class MutableResourceCollectionBase(ResourceCollectionBase):

    def _create_member(self, values):
        """Create a new member of this collection.

        :param values: Fields to set on creation.
        :return: Created resource or None if it was not returned by the server.
        """
        response = self._conn.post(self._path, data=values)
        self.invalidate(force_refresh=True)

        location = response.headers.get('Location')
        if location is None:
            return None

        return self._resource_type(
            self._conn, location,
            redfish_version=self.redfish_version,
            registries=self.registries,
            root=self.root)

    def delete_member(self, identity):
        """Delete the given member of the collection."""
        self._conn.delete(identity)
        self.invalidate(force_refresh=True)