Debian Codegraph @

os_vif/objects/host_info.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 from oslo_utils import versionutils
14 from oslo_versionedobjects import base
15 from oslo_versionedobjects import fields
16
17 from os_vif import exception
18 from os_vif.objects import base as osv_base
19
20
21 def _get_common_version(object_name, max_version, min_version, exc_notmatch,
22 exc_notsupported):
23 """Returns the accepted version from the loaded OVO registry"""
24 reg = base.VersionedObjectRegistry.obj_classes()
25
26 if object_name not in reg:
27 raise exc_notmatch(name=object_name)
28
29 gotvers = []
30 for regobj in reg[object_name]:
31 gotvers.append(regobj.VERSION)
32 got = versionutils.convert_version_to_tuple(regobj.VERSION)
33 minwant = versionutils.convert_version_to_tuple(min_version)
34 maxwant = versionutils.convert_version_to_tuple(max_version)
35
36 if minwant <= got <= maxwant:
37 return regobj.VERSION
38
39 raise exc_notsupported(name=object_name,
40 got_versions=",".join(gotvers),
41 min_version=min_version,
42 max_version=max_version)
43
44
45 @base.VersionedObjectRegistry.register
46 class HostPortProfileInfo(osv_base.VersionedObject,
47 base.ComparableVersionedObject,
48 osv_base.VersionedObjectPrintableMixin):
49 """
50 Class describing a PortProfile class and its supported versions
51 """
52 # Version 1.0: Initial version
53 VERSION = "1.0"
54
55 fields = {
56 # object name of the subclass of os_vif.objects.vif.VIFPortProfileBase
57 "profile_object_name": fields.StringField(),
58
59 # String representing the earliest version of @name
60 # that the plugin understands
61 "min_version": fields.StringField(),
62
63 # String representing the latest version of @name
64 # that the plugin understands
65 "max_version": fields.StringField(),
66 }
67
68 def get_common_version(self):
69 return _get_common_version(self.profile_object_name,
70 self.max_version,
71 self.min_version,
72 exception.NoMatchingPortProfileClass,
73 exception.NoSupportedPortProfileVersion)
74
75
76 @base.VersionedObjectRegistry.register
77 class HostVIFInfo(osv_base.VersionedObject, base.ComparableVersionedObject,
78 osv_base.VersionedObjectPrintableMixin):
79 """
80 Class describing a VIF class and its supported versions
81 """
82 # Version 1.0: Initial version
83 # Version 1.1: Adds 'supported_port_profiles' field
84 VERSION = "1.1"
85
86 fields = {
87 # object name of the subclass of os_vif.objects.vif.VIFBase
88 "vif_object_name": fields.StringField(),
89
90 # String representing the earliest version of @name
91 # that the plugin understands
92 "min_version": fields.StringField(),
93
94 # String representing the latest version of @name
95 # that the plugin understands
96 "max_version": fields.StringField(),
97
98 # list of supported PortProfile objects and versions.
99 "supported_port_profiles": fields.ListOfObjectsField(
100 "HostPortProfileInfo")
101 }
102
103 def obj_make_compatible(self, primitive, target_version):
104 target_version = versionutils.convert_version_to_tuple(target_version)
105 if target_version < (1, 1) and 'supported_port_profiles' in primitive:
106 del primitive['supported_port_profiles']
107 super(HostVIFInfo, self).obj_make_compatible(primitive, '1.0')
108
109 def get_common_version(self):
110 return _get_common_version(self.vif_object_name,
111 self.max_version,
112 self.min_version,
113 exception.NoMatchingVIFClass,
114 exception.NoSupportedVIFVersion)
115
116
117 @base.VersionedObjectRegistry.register
118 class HostPluginInfo(osv_base.VersionedObject, base.ComparableVersionedObject,
119 osv_base.VersionedObjectPrintableMixin):
120 """
121 Class describing a plugin and its supported VIF classes
122 """
123 # Version 1.0: Initial version
124 VERSION = "1.0"
125
126 fields = {
127 # name of the plugin
128 "plugin_name": fields.StringField(),
129
130 # list of HostVIFInfo instances supported by the plugin
131 "vif_info": fields.ListOfObjectsField("HostVIFInfo"),
132 }
133
134 def has_vif(self, name):
135 for vif in self.vif_info:
136 if vif.vif_object_name == name:
137 return True
138 return False
139
140 def get_vif(self, name):
141 for vif in self.vif_info:
142 if vif.vif_object_name == name:
143 return vif
144
145 raise exception.NoMatchingVIFClass(vif_name=name)
146
147 def filter_vif_types(self, permitted_vif_type_names):
148 new_vif_info = []
149 for vif in self.vif_info:
150 if vif.vif_object_name in permitted_vif_type_names:
151 new_vif_info.append(vif)
152 self.vif_info = new_vif_info
153
154
155 @base.VersionedObjectRegistry.register
156 class HostInfo(osv_base.VersionedObject, base.ComparableVersionedObject,
157 osv_base.VersionedObjectPrintableMixin):
158 """
159 Class describing a host host and its supported plugin classes
160 """
161
162 fields = {
163 # list of HostPluginInfo instances supported by the host host
164 "plugin_info": fields.ListOfObjectsField("HostPluginInfo"),
165 }
166
167 def has_plugin(self, name):
168 for plugin in self.plugin_info:
169 if name == plugin.plugin_name:
170 return True
171 return False
172
173 def get_plugin(self, name):
174 for plugin in self.plugin_info:
175 if name == plugin.plugin_name:
176 return plugin
177
178 raise exception.NoMatchingPlugin(plugin_name=name)
179
180 def filter_vif_types(self, permitted_vif_type_names):
181 new_plugins = []
182 for plugin in self.plugin_info:
183 plugin.filter_vif_types(permitted_vif_type_names)
184 if len(plugin.vif_info) == 0:
185 continue
186 new_plugins.append(plugin)
187 self.plugin_info = new_plugins