Module: OMF::SFA::Resource::Base::ClassMethods
- Included in:
- AbstractService, CompGroup, DiskImage, GroupComponent, Ip, OComponent, OLease, SliverType
- Defined in:
- lib/omf-sfa/resource/sfa_base.rb
Constant Summary collapse
- @@sfa_defs =
{}
- @@sfa_namespaces =
{}
- @@sfa_namespace2prefix =
{}
- @@sfa_classes =
{}
- @@sfa_name2class =
{}
- @@sfa_suppress_id =
{}
- @@sfa_suppress_uuid =
{}
Instance Method Summary collapse
- #_sfa_add_ns(name, opts = {}) ⇒ Object
- #_sfa_prefix_for_namespace(urn) ⇒ Object
- #_to_sfa_xml(resources, root, obj2id, opts = {}) ⇒ Object
- #default_component_manager_id ⇒ Object
- #default_domain ⇒ Object
- #descendants ⇒ Object
- #from_sfa(resource_el, context = {}, type = 'manifest') ⇒ Object
-
#sfa(name, opts = {}) ⇒ Object
Define a SFA property.
- #sfa_add_namespace(prefix, urn, options = {}) ⇒ Object
- #sfa_add_namespaces_to_document(doc) ⇒ Object
- #sfa_cast_property_value(value, property_name, context, type = nil) ⇒ Object
- #sfa_class(name = nil, opts = {}) ⇒ Object
- #sfa_def_for(name) ⇒ Object
-
#sfa_defs(cached = true) ⇒ Object
Return all the property definitions for this class.
- #sfa_suppress_id ⇒ Object
- #sfa_suppress_id? ⇒ Boolean
- #sfa_suppress_uuid ⇒ Object
- #sfa_suppress_uuid? ⇒ Boolean
-
#to_rspec(resources, type, opts = {}) ⇒ Object
opts: :valid_for - valid [sec] from now.
Instance Method Details
#_sfa_add_ns(name, opts = {}) ⇒ Object
294 295 296 297 298 299 300 301 302 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 294 def _sfa_add_ns(name, opts = {}) if ns = opts[:namespace] unless @@sfa_namespaces[ns] raise "Unknown namespace '#{ns}'" end name = "#{ns}:#{name}" end name end |
#_sfa_prefix_for_namespace(urn) ⇒ Object
59 60 61 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 59 def _sfa_prefix_for_namespace(urn) @@sfa_namespace2prefix[urn] end |
#_to_sfa_xml(resources, root, obj2id, opts = {}) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 224 def _to_sfa_xml(resources, root, obj2id, opts = {}) #puts "RRRXXX> #{resources}" if resources.kind_of? Enumerable #puts "RRRXXX2> #{resources}" resources.each do |r| #puts "R3> #{r}" _to_sfa_xml(r, root, obj2id, opts) end # elsif resources.kind_of? OMF::SFA::Resource::OGroup # # NOTE: Should be adding a GROUP element! # resources.each_resource do |r| # _to_sfa_xml(r, root, obj2id, opts) # end else resources.to_sfa_xml(root, obj2id, opts) end root.document end |
#default_component_manager_id ⇒ Object
25 26 27 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 25 def default_component_manager_id() Constants.default_component_manager_id end |
#default_domain ⇒ Object
21 22 23 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 21 def default_domain() Constants.default_domain end |
#descendants ⇒ Object
304 305 306 307 308 309 310 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 304 def descendants result = [] ObjectSpace.each_object(Class) do |klass| result = result << klass if klass < self end result end |
#from_sfa(resource_el, context = {}, type = 'manifest') ⇒ Object
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 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 142 def from_sfa(resource_el, context = {}, type = 'manifest') resource = nil uuid = nil comp_gurn = nil unless (href = resource_el.namespace.href) == SFA_NAMESPACE_URI unless prefix = _sfa_prefix_for_namespace(href) warn "Ignoring unknown element '#{resource_el.name}' - NS: '#{resource_el.namespace.href}'" return end ns_opts = @@sfa_namespaces[prefix] return if ns_opts[:ignore] end client_id_attr = resource_el.attributes['client_id'] client_id = client_id_attr ? client_id_attr.value : nil if uuid_attr = (resource_el.attributes['uuid'] || resource_el.attributes['idref']) uuid = UUIDTools::UUID.parse(uuid_attr.value) if resource = OMF::SFA::Resource::OResource.first(:uuid => uuid) context[client_id] = resource if client_id return resource.from_sfa(resource_el, context, type) end end # TODO: Clarify the role of 'sliver_id' vs. 'component_id' if comp_id_attr = resource_el.attributes['sliver_id'] || resource_el.attributes['component_id'] comp_id = comp_id_attr.value comp_gurn = OMF::SFA::Resource::GURN.parse(comp_id) #begin if uuid = comp_gurn.uuid resource = OMF::SFA::Resource::OResource.first(:uuid => uuid) context[client_id] = resource if client_id return resource.from_sfa(resource_el, context, type) end if resource = OMF::SFA::Resource::OComponent.first(:urn => comp_gurn) context[client_id] = resource if client_id return resource.from_sfa(resource_el, context, type) end else # need to create a comp_gurn (the link is an example of that) unless client_id raise "Need 'client_id' for resource '#{resource_el}'" end if sliver_id_attr = resource_el.attributes['sliver_id'] sliver_gurn = OMF::SFA::Resource::GURN.parse(sliver_id_attr.value) #puts "SLIVER_ID name: #{sliver_gurn.name} short: #{sliver_gurn.short_name} uuid: #{sliver_gurn.uuid}" else if type == 'request' sliver_gurn = OMF::SFA::Resource::GURN.create(client_id, type: resource_el.name, domain: 'unknown') else raise "Need 'sliver_id' for resource '#{resource_el}'" end end #puts "TYPE: #{type} - #{sliver_gurn}" # opts = { # :domain => sliver_gurn.domain, # :type => resource_el.name # TODO: This most likely will break with NS # } # comp_gurn = OMF::SFA::Resource::GURN.create("#{sliver_gurn.short_name}:#{client_id}", opts) comp_gurn = sliver_gurn if resource = OMF::SFA::Resource::OComponent.first(:urn => comp_gurn) context[client_id] = resource if client_id resource.from_sfa(resource_el, context, type) resource.save return end end # Appears the resource doesn't exist yet, let's see if we can create one type = resource_el.name #comp_gurn.type if res_class = @@sfa_name2class[type] resource = res_class.new(:name => comp_gurn.short_name, :urn => comp_gurn) #puts ">>> #{comp_gurn} - #{resource.to_hash}" context[client_id] = resource if client_id resource.from_sfa(resource_el, context, type) #puts "22>>> #{resource.to_hash}" return end raise "Unknown resource type '#{type}' (#{@@sfa_name2class.keys.join(', ')})" end |
#sfa(name, opts = {}) ⇒ Object
Define a SFA property
97 98 99 100 101 102 103 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 97 def sfa(name, opts = {}) name = name.to_s props = sfa_defs() # get all the sfa properties of this class props[name] = opts # recalculate sfa properties of the descendants descendants.each do |c| c.sfa_defs(false) end end |
#sfa_add_namespace(prefix, urn, options = {}) ⇒ Object
53 54 55 56 57 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 53 def sfa_add_namespace(prefix, urn, = {}) [:urn] = urn @@sfa_namespaces[prefix] = @@sfa_namespace2prefix[urn] = prefix end |
#sfa_add_namespaces_to_document(doc) ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 63 def sfa_add_namespaces_to_document(doc) root = doc.root root.add_namespace(nil, SFA_NAMESPACE_URI) root.add_namespace('omf', OMF_NAMESPACE_URI) @@sfa_namespaces.each do |name, opts| root.add_namespace(name.to_s, opts[:urn]) #'omf', 'http://tenderlovemaking.com') end end |
#sfa_cast_property_value(value, property_name, context, type = nil) ⇒ Object
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 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 267 def sfa_cast_property_value(value, property_name, context, type = nil) name = property_name.to_s unless type pdef = sfa_def_for(name) raise "Unknow SFA property '#{name}'" unless pdef type = pdef[:type] end if type.kind_of?(Symbol) if type == :boolean unless value.kind_of?(TrueClass) || value.kind_of?(FalseClass) raise "Wrong type for '#{name}', is #{value.type}, but should be #{type}" end else raise "Unknown type '#{type}', use real Class" end elsif !(value.kind_of?(type)) if type.respond_to? :sfa_create value = type.sfa_create(value, context) else raise "Wrong type for '#{name}', is #{value.class}, but should be #{type}" end # puts "XXX>>> #{name}--#{! value.kind_of?(type)}--#{value.class}||#{type}||#{pdef.inspect}" end value end |
#sfa_class(name = nil, opts = {}) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 41 def sfa_class(name = nil, opts = {}) if name name = _sfa_add_ns(name, opts) #sfa_defs()['_class_'] = name @@sfa_classes[self] = name @@sfa_name2class[name] = self else @@sfa_classes[self] #sfa_def_for('_class_') end end |
#sfa_def_for(name) ⇒ Object
263 264 265 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 263 def sfa_def_for(name) sfa_defs()[name.to_s] end |
#sfa_defs(cached = true) ⇒ Object
Return all the property definitions for this class.
cached
- If false, recalculate
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 247 def sfa_defs(cached = true) unless cached && props = @@sfa_defs[self] # this assumes that all the properties of the super classes are already set props = {} klass = self while klass = klass.superclass if sp = @@sfa_defs[klass] props = sp.merge(props) end end #puts "PROP #{self}:#{props.keys.inspect}" @@sfa_defs[self] = props end props end |
#sfa_suppress_id ⇒ Object
72 73 74 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 72 def sfa_suppress_id @@sfa_suppress_id[self] = true end |
#sfa_suppress_id? ⇒ Boolean
76 77 78 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 76 def sfa_suppress_id? @@sfa_suppress_id[self] == true end |
#sfa_suppress_uuid ⇒ Object
80 81 82 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 80 def sfa_suppress_uuid @@sfa_suppress_uuid[self] = true end |
#sfa_suppress_uuid? ⇒ Boolean
84 85 86 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 84 def sfa_suppress_uuid? @@sfa_suppress_uuid[self] == true end |
#to_rspec(resources, type, opts = {}) ⇒ Object
opts:
:valid_for - valid [sec] from now
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 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 108 def to_rspec(resources, type, opts = {}) doc = Nokogiri::XML::Document.new #<rspec expires="2011-09-13T09:07:09Z" generated="2011-09-13T09:07:09Z" type="advertisement" xmlns="http://www.geni.net/resources/rspec/3" xmlns:ol="http://nitlab.inf.uth.gr/schema/sfa/rspec/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.geni.net/resources/rspec/3 http://www.geni.net/resources/rspec/3/ad.xsd http://nitlab.inf.uth.gr/schema/sfa/rspec/1 http://nitlab.inf.uth.gr/schema/sfa/rspec/1/ad-reservation.xsd"> root = doc.add_child(Nokogiri::XML::Element.new('rspec', doc)) root.set_attribute('type', type) now = Time.now root.set_attribute('generated', now.iso8601) case opts[:type] = type = type.to_sym when :manifest schema = 'manifest.xsd' when :advertisement schema = 'ad.xsd' root.set_attribute('expires', (now + (opts[:valid_for] || 600)).iso8601) when :request schema = 'request.xsd' else raise "Unnown Rspec type '#{type}'" end root.add_namespace(nil, SFA_NAMESPACE_URI) root.add_namespace('xsi', "http://www.w3.org/2001/XMLSchema-instance") #root['xsi:schemaLocation'] = "#{SFA_NAMESPACE_URI} #{SFA_NAMESPACE_URI}/#{schema} #{@@sfa_namespaces[:ol]} #{@@sfa_namespaces[:ol]}/ad-reservation.xsd" @@sfa_namespaces.each do |prefix, opts| root.add_namespace(prefix.to_s, opts[:urn]) end #root = doc.create_element('rspec', doc) #doc.add_child root obj2id = {} _to_sfa_xml(resources, root, obj2id, opts) end |