Coverage for nova/objects/resource.py: 100%

40 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-04-17 15:08 +0000

1# Copyright 2019 Intel Inc. 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); you may 

4# not use this file except in compliance with the License. You may obtain 

5# a copy of the License at 

6# 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 

11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 

12# License for the specific language governing permissions and limitations 

13# under the License. 

14 

15from oslo_serialization import jsonutils 

16 

17from nova.db.main import api as db 

18from nova.objects import base 

19from nova.objects import fields 

20 

21 

22@base.NovaObjectRegistry.register 

23class ResourceMetadata(base.NovaObject): 

24 # Version 1.0: Initial version 

25 VERSION = "1.0" 

26 

27 # This is parent object of specific resources. 

28 # And it's used to be a object field of Resource, 

29 # that is to say Resource.metadata. 

30 

31 def __eq__(self, other): 

32 return base.all_things_equal(self, other) 

33 

34 def __ne__(self, other): 

35 return not (self == other) 

36 

37 

38@base.NovaObjectRegistry.register 

39class Resource(base.NovaObject): 

40 # Version 1.0: Initial version 

41 VERSION = "1.0" 

42 

43 fields = { 

44 # UUID of resource provider 

45 'provider_uuid': fields.UUIDField(), 

46 # resource class of the Resource 

47 'resource_class': fields.ResourceClassField(), 

48 # identifier is used to identify resource, it is up to virt drivers 

49 # for mdev, it will be a UUID, for vpmem, it's backend namespace name 

50 'identifier': fields.StringField(), 

51 # metadata is used to contain virt driver specific resource info 

52 'metadata': fields.ObjectField('ResourceMetadata', subclasses=True), 

53 } 

54 

55 def __eq__(self, other): 

56 return base.all_things_equal(self, other) 

57 

58 def __ne__(self, other): 

59 return not (self == other) 

60 

61 def __hash__(self): 

62 metadata = self.metadata if 'metadata' in self else None 

63 return hash((self.provider_uuid, self.resource_class, 

64 self.identifier, metadata)) 

65 

66 

67@base.NovaObjectRegistry.register 

68class ResourceList(base.ObjectListBase, base.NovaObject): 

69 # Version 1.0: Initial version 

70 VERSION = "1.0" 

71 

72 fields = { 

73 'objects': fields.ListOfObjectsField('Resource'), 

74 } 

75 

76 @base.remotable_classmethod 

77 def get_by_instance_uuid(cls, context, instance_uuid): 

78 db_extra = db.instance_extra_get_by_instance_uuid( 

79 context, instance_uuid, columns=['resources']) 

80 if not db_extra or db_extra['resources'] is None: 

81 return None 

82 

83 primitive = jsonutils.loads(db_extra['resources']) 

84 resources = cls.obj_from_primitive(primitive) 

85 return resources 

86 

87 

88@base.NovaObjectRegistry.register 

89class LibvirtVPMEMDevice(ResourceMetadata): 

90 # Version 1.0: Initial version 

91 VERSION = "1.0" 

92 

93 fields = { 

94 # This is configured in file, used to generate resource class name 

95 # CUSTOM_PMEM_NAMESPACE_$LABEL 

96 'label': fields.StringField(), 

97 # Backend pmem namespace's name 

98 'name': fields.StringField(), 

99 # Backend pmem namespace's size 

100 'size': fields.IntegerField(), 

101 # Backend device path 

102 'devpath': fields.StringField(), 

103 # Backend pmem namespace's alignment 

104 'align': fields.IntegerField(), 

105 } 

106 

107 def __hash__(self): 

108 # Be sure all fields are set before using hash method 

109 return hash((self.label, self.name, self.size, 

110 self.devpath, self.align))