omf_sfa 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/bin/parse_rspec.rb CHANGED
@@ -12,7 +12,7 @@ require 'rubygems'
12
12
  require 'optparse'
13
13
  require 'dm-core'
14
14
  require 'nokogiri'
15
- require 'omf_common/lobject'
15
+ require 'omf_base/lobject'
16
16
  require 'json'
17
17
 
18
18
  $verbose = false
@@ -40,7 +40,7 @@ end
40
40
  op.on_tail('-h', "--help", "Show this message") { $stderr.puts op; exit }
41
41
  rest = op.parse(ARGV) || []
42
42
 
43
- OMF::Common::Loggable.init_log 'parse_rspec'
43
+ OMF::Base::Loggable.init_log 'parse_rspec'
44
44
 
45
45
  unless in_file_name = (rest || [])[0]
46
46
  abort "Missing rspec file"
@@ -69,6 +69,7 @@ DataMapper.finalize
69
69
  # Process RSPEC
70
70
  resources = []
71
71
  context = {}
72
+ rspec_type = rspec.attributes['type'].value
72
73
  rspec.children.each do |el|
73
74
  next if el.is_a? Nokogiri::XML::Text
74
75
  if $verbose
@@ -77,7 +78,7 @@ rspec.children.each do |el|
77
78
  end
78
79
  n = nil
79
80
  begin
80
- n = OMF::SFA::Resource::OComponent.from_sfa(el, context)
81
+ n = OMF::SFA::Resource::OComponent.from_sfa(el, context, rspec_type)
81
82
  rescue Exception => ex
82
83
  puts "WARN: Couldn't parse '#{el.to_s[0 .. 30]}' - #{ex}"
83
84
  puts ex.backtrace
@@ -109,7 +110,7 @@ end
109
110
 
110
111
  def print_json(resources)
111
112
  resources.each do |r|
112
- puts '================='
113
+ puts '=================' if $verbose
113
114
  r.urn
114
115
  #puts "#{r.type} (#{r.inspect})"\n
115
116
  puts "#{r.type}"
@@ -160,7 +161,7 @@ if $out_file_name
160
161
  f = File.open($out_file_name, 'w')
161
162
  f.write out
162
163
  else
163
- puts '=========================='
164
+ puts '==========================' if $verbose
164
165
  puts out
165
166
  end
166
167
 
@@ -4,6 +4,7 @@ require 'nokogiri'
4
4
  require 'uuid'
5
5
  require 'set'
6
6
  require 'json'
7
+ require 'thin/async'
7
8
 
8
9
  require 'omf_base/lobject'
9
10
 
@@ -119,6 +120,9 @@ module OMF::SFA::AM::Rest
119
120
  }, ""]
120
121
  end
121
122
  content_type, body = dispatch(req)
123
+ if body.is_a? Thin::AsyncResponse
124
+ return body.finish
125
+ end
122
126
  if req['_format'] == 'html'
123
127
  #body = self.class.convert_to_html(body, env, Set.new((@coll_handlers || {}).keys))
124
128
  body = convert_to_html(body, env, {}, Set.new((@coll_handlers || {}).keys))
@@ -219,7 +223,7 @@ module OMF::SFA::AM::Rest
219
223
  resource_id = opts[:resource_uri] = path.shift
220
224
  opts[:resource] = nil
221
225
  if resource_id
222
- resource = opts[:resource] = find_resource(resource_id)
226
+ resource = opts[:resource] = find_resource(resource_id, {}, opts)
223
227
  end
224
228
  return self if path.empty?
225
229
 
@@ -402,7 +406,7 @@ module OMF::SFA::AM::Rest
402
406
 
403
407
 
404
408
 
405
- def find_resource(resource_uri, description = {})
409
+ def find_resource(resource_uri, description = {}, opts = {})
406
410
  descr = description.dup
407
411
  descr.delete(:resource_uri)
408
412
  if UUID.validate(resource_uri)
@@ -95,11 +95,20 @@ module OMF::SFA::Resource
95
95
 
96
96
  def uuid
97
97
  unless @uuid
98
- @uuid = UUIDTools::UUID.parse(short_name)
98
+ begin
99
+ @uuid = UUIDTools::UUID.parse(short_name)
100
+ rescue ArgumentError
101
+ if (p = short_name.split(':')).length > 1
102
+ # ExoGeni has short names of the form uuid:short_name
103
+ # TODO: This turned out to be not a sliver UUID, but something else
104
+ # begin
105
+ # @uuid = UUIDTools::UUID.parse(p[0])
106
+ # rescue ArgumentError
107
+ # end
108
+ end
109
+ end
99
110
  end
100
111
  @uuid
101
- rescue ArgumentError
102
- @uuid = nil
103
112
  end
104
113
 
105
114
  def to_s
@@ -8,6 +8,7 @@ module OMF::SFA::Resource
8
8
  #property :hardware_type, String
9
9
  oproperty :role, String
10
10
  oproperty :node, :node
11
+ oproperty :mac_address, String
11
12
  oproperty :channel, :channel
12
13
  oproperty :ip_addresses, OMF::SFA::Resource::Ip, :functional => false
13
14
 
@@ -23,6 +24,7 @@ module OMF::SFA::Resource
23
24
  #sfa :public_ipv4, :ip4, :attribute => true
24
25
  sfa :role, :attribute => true
25
26
  sfa :ip
27
+ sfa :mac_address, :attribute => true
26
28
 
27
29
  # @see IComponent
28
30
  #
@@ -12,6 +12,7 @@ module OMF::SFA::Resource
12
12
  #oproperty :sliver_type, String, :required => false
13
13
  oproperty :interfaces, :interface, :functional => false
14
14
  oproperty :exclusive, Boolean, :default => true
15
+
15
16
  #belongs_to :sliver
16
17
 
17
18
  sfa_class 'node'
@@ -19,7 +20,8 @@ module OMF::SFA::Resource
19
20
  sfa :available, :attr_value => 'now' # <available now="true">
20
21
  #sfa :sliver_type, :attr_value => 'name'
21
22
  sfa :interfaces, :inline => true, :has_many => true
22
- # sfa :exclusive, :attribute => true
23
+ sfa :exclusive, :attribute => true
24
+
23
25
 
24
26
  # Override xml serialization of 'interface'
25
27
  def _to_sfa_property_xml(pname, value, res_el, pdef, obj2id, opts)
@@ -54,6 +56,7 @@ module OMF::SFA::Resource
54
56
  end
55
57
  end
56
58
 
59
+
57
60
  def xx_to_sfa_interfaces_property_hash(interfaces, pdef, href2obj, opts)
58
61
  # opts = opts.dup
59
62
  # opts[:href_prefix] = (opts[:href_prefix] || '/') + 'interfaces/'
@@ -10,8 +10,9 @@ module OMF::SFA::Resource
10
10
  #
11
11
  class OComponent < OResource
12
12
 
13
- oproperty :domain, String #, readonly => true
14
- oproperty :exclusive, DataMapper::Property::Boolean
13
+ #oproperty :domain, String #, readonly => true
14
+
15
+ oproperty :component_gurn, OMF::SFA::Resource::GURN
15
16
  oproperty :component_manager_gurn, OMF::SFA::Resource::GURN
16
17
 
17
18
  # Status of component. Should be any of configuring, ready, failed, and unknown
@@ -26,6 +27,8 @@ module OMF::SFA::Resource
26
27
 
27
28
  oproperty :account, :account, :inverse => :active_components
28
29
 
30
+ oproperty :expiration_time, Time
31
+
29
32
  has n, :component_leases, :child_key => [:component_id]
30
33
  has n, :leases, :model => 'OLease', :through => :component_leases, :via => :lease
31
34
 
@@ -33,9 +36,12 @@ module OMF::SFA::Resource
33
36
  include OMF::SFA::Resource::Base::InstanceMethods
34
37
 
35
38
  sfa_add_namespace :omf, 'http://schema.mytestbed.net/sfa/rspec/1'
39
+ sfa_add_namespace :exo_sliver, "http://groups.geni.net/exogeni/attachment/wiki/RspecExtensions/sliver-info/1"
40
+ sfa_add_namespace :exo_slice, "http://groups.geni.net/exogeni/attachment/wiki/RspecExtensions/slice-info/1", ignore: true
41
+
36
42
  #sfa_add_namespace :ol, 'http://nitlab.inf.uth.gr/schema/sfa/rspec/1'
37
43
 
38
- sfa :component_id, :attribute => true, :prop_name => :urn # "urn:publicid:IDN+plc:cornell+node+planetlab3-dsl.cs.cornell.edu"
44
+ sfa :component_id, :attribute => true, :prop_name => :component_gurn # "urn:publicid:IDN+plc:cornell+node+planetlab3-dsl.cs.cornell.edu"
39
45
 
40
46
  sfa :client_id, :attribute => true, :prop_name => :name
41
47
  alias_method :client_id, :name
@@ -44,6 +50,8 @@ module OMF::SFA::Resource
44
50
  sfa :component_name, :attribute => true # "plane
45
51
  sfa :leases, :inline => true, :has_many => true
46
52
 
53
+ sfa :exo_sliver__geni_sliver_info
54
+
47
55
  #def component_id
48
56
  # res = oproperty_get(:id)
49
57
  #end
@@ -85,6 +93,17 @@ module OMF::SFA::Resource
85
93
  super
86
94
  end
87
95
 
96
+ def _from_sfa_exo_sliver__geni_sliver_info_property_xml(resource_el, props, context)
97
+ resource_el.children.filter("//*[local-name()='geni_sliver_info']").each do |si|
98
+ #puts ">>>> #{si.attributes}"
99
+ if value = si["expiration_time"]
100
+ self.expiration_time = Time.parse(value)
101
+ end
102
+ if value = si["state"]
103
+ self.status = value
104
+ end
105
+ end
106
+ end
88
107
 
89
108
  def clone
90
109
  clone = super
@@ -122,7 +141,7 @@ module OMF::SFA::Resource
122
141
  end
123
142
 
124
143
  before :save do
125
- self.urn = GURN.create(self.name, self)
144
+ #self.urn = GURN.create(self.name, self) unless self.urn
126
145
  end
127
146
 
128
147
  def destroy!
@@ -299,7 +299,7 @@ module OMF::SFA::Resource
299
299
  self
300
300
  end
301
301
 
302
- [:each, :each_with_index, :select, :map].each do |n|
302
+ [:each, :each_with_index, :select, :map, :find].each do |n|
303
303
  define_method n do |&block|
304
304
  #c = OProperty.all(name: @name, o_resource: @resource)
305
305
  c = self.to_a()
@@ -341,16 +341,18 @@ module OMF::SFA::Resource
341
341
  unless self.name
342
342
  self.name = self.urn ? GURN.create(self.urn).short_name : "r#{self.object_id}"
343
343
  end
344
- unless self.urn
345
- # The purpose or function of a URN is to provide a globally unique,
346
- # persistent identifier used for recognition, for access to
347
- # characteristics of the resource or for access to the resource
348
- # itself.
349
- # source: http://tools.ietf.org/html/rfc1737
350
- #
351
- name = self.name
352
- self.urn = GURN.create(name, :model => self.class).to_s
353
- end
344
+ # The uuid already provides a unique identifier. The URN is a more readable one, but
345
+ # it doesn't make sense for each resource, so leave it to the creator to set one or not.
346
+ # unless self.urn
347
+ # # The purpose or function of a URN is to provide a globally unique,
348
+ # # persistent identifier used for recognition, for access to
349
+ # # characteristics of the resource or for access to the resource
350
+ # # itself.
351
+ # # source: http://tools.ietf.org/html/rfc1737
352
+ # #
353
+ # name = self.name
354
+ # self.urn = GURN.create(name, :model => self.class).to_s
355
+ # end
354
356
  end
355
357
 
356
358
  def destroy
@@ -449,6 +451,7 @@ module OMF::SFA::Resource
449
451
  end
450
452
 
451
453
  def to_hash_long(h, objs = {}, opts = {})
454
+ h[:urn] = self.urn if self.urn
452
455
  _oprops_to_hash(h, objs, opts)
453
456
  h
454
457
  end
@@ -27,6 +27,7 @@ module OMF::SFA
27
27
 
28
28
  @@sfa_defs = {}
29
29
  @@sfa_namespaces = {}
30
+ @@sfa_namespace2prefix = {}
30
31
  @@sfa_classes = {}
31
32
  @@sfa_name2class = {}
32
33
 
@@ -46,15 +47,21 @@ module OMF::SFA
46
47
  end
47
48
  end
48
49
 
49
- def sfa_add_namespace(prefix, urn)
50
- @@sfa_namespaces[prefix] = urn
50
+ def sfa_add_namespace(prefix, urn, options = {})
51
+ options[:urn] = urn
52
+ @@sfa_namespaces[prefix] = options
53
+ @@sfa_namespace2prefix[urn] = prefix
54
+ end
55
+
56
+ def _sfa_prefix_for_namespace(urn)
57
+ @@sfa_namespace2prefix[urn]
51
58
  end
52
59
 
53
60
  def sfa_add_namespaces_to_document(doc)
54
61
  root = doc.root
55
62
  root.add_namespace(nil, SFA_NAMESPACE_URI)
56
- @@sfa_namespaces.each do |name, uri|
57
- root.add_namespace(name.to_s, uri) #'omf', 'http://tenderlovemaking.com')
63
+ @@sfa_namespaces.each do |name, opts|
64
+ root.add_namespace(name.to_s, opts[:urn]) #'omf', 'http://tenderlovemaking.com')
58
65
  end
59
66
  end
60
67
 
@@ -92,8 +99,8 @@ module OMF::SFA
92
99
  schema = 'ad.xsd'
93
100
  end
94
101
  root['xsi:schemaLocation'] = "#{SFA_NAMESPACE_URI} #{SFA_NAMESPACE_URI}/#{schema} #{@@sfa_namespaces[:ol]} #{@@sfa_namespaces[:ol]}/ad-reservation.xsd"
95
- @@sfa_namespaces.each do |prefix, urn|
96
- root.add_namespace(prefix.to_s, urn)
102
+ @@sfa_namespaces.each do |prefix, opts|
103
+ root.add_namespace(prefix.to_s, opts[:urn])
97
104
  end
98
105
 
99
106
  root.set_attribute('type', opts[:type])
@@ -107,14 +114,18 @@ module OMF::SFA
107
114
  _to_sfa_xml(resources, root, obj2id, opts)
108
115
  end
109
116
 
110
- def from_sfa(resource_el, context = {})
117
+ def from_sfa(resource_el, context = {}, type = 'manifest')
111
118
  resource = nil
112
119
  uuid = nil
113
120
  comp_gurn = nil
114
121
 
115
- unless resource_el.namespace.href == SFA_NAMESPACE_URI
116
- warn "WARNING: '#{resource_el.name}' Can't handle non-default namespaces '#{resource_el.namespace.href}'"
117
- return
122
+ unless (href = resource_el.namespace.href) == SFA_NAMESPACE_URI
123
+ unless prefix = _sfa_prefix_for_namespace(href)
124
+ warn "Ignoring unknown element '#{resource_el.name}' - NS: '#{resource_el.namespace.href}'"
125
+ return
126
+ end
127
+ ns_opts = @@sfa_namespaces[prefix]
128
+ return if ns_opts[:ignore]
118
129
  end
119
130
 
120
131
  client_id_attr = resource_el.attributes['client_id']
@@ -124,49 +135,63 @@ module OMF::SFA
124
135
  uuid = UUIDTools::UUID.parse(uuid_attr.value)
125
136
  if resource = OMF::SFA::Resource::OResource.first(:uuid => uuid)
126
137
  context[client_id] = resource if client_id
127
- return resource.from_sfa(resource_el, context)
138
+ return resource.from_sfa(resource_el, context, type)
128
139
  end
129
140
  end
130
141
 
131
- if comp_id_attr = resource_el.attributes['component_id']
142
+ # TODO: Clarify the role of 'sliver_id' vs. 'component_id'
143
+ if comp_id_attr = resource_el.attributes['sliver_id'] || resource_el.attributes['component_id']
132
144
  comp_id = comp_id_attr.value
133
145
  comp_gurn = OMF::SFA::Resource::GURN.parse(comp_id)
134
146
  #begin
135
147
  if uuid = comp_gurn.uuid
136
148
  resource = OMF::SFA::Resource::OResource.first(:uuid => uuid)
137
149
  context[client_id] = resource if client_id
138
- return resource.from_sfa(resource_el, context)
150
+ return resource.from_sfa(resource_el, context, type)
139
151
  end
140
152
  if resource = OMF::SFA::Resource::OComponent.first(:urn => comp_gurn)
141
153
  context[client_id] = resource if client_id
142
- return resource.from_sfa(resource_el, context)
154
+ return resource.from_sfa(resource_el, context, type)
143
155
  end
144
156
  else
145
157
  # need to create a comp_gurn (the link is an example of that)
146
- unless sliver_id_attr = resource_el.attributes['sliver_id']
147
- raise "Need 'sliver_id' for resource '#{resource_el}'"
148
- end
149
- sliver_gurn = OMF::SFA::Resource::GURN.parse(sliver_id_attr.value)
150
158
  unless client_id
151
159
  raise "Need 'client_id' for resource '#{resource_el}'"
152
160
  end
153
- opts = {
154
- :domain => sliver_gurn.domain,
155
- :type => resource_el.name # TODO: This most likely will break with NS
156
- }
157
- comp_gurn = OMF::SFA::Resource::GURN.create("#{sliver_gurn.short_name}:#{client_id}", opts)
161
+ if sliver_id_attr = resource_el.attributes['sliver_id']
162
+ sliver_gurn = OMF::SFA::Resource::GURN.parse(sliver_id_attr.value)
163
+ #puts "SLIVER_ID name: #{sliver_gurn.name} short: #{sliver_gurn.short_name} uuid: #{sliver_gurn.uuid}"
164
+ else
165
+ if type == 'request'
166
+ sliver_gurn = OMF::SFA::Resource::GURN.create(client_id, type: resource_el.name, domain: 'unknown')
167
+ else
168
+ raise "Need 'sliver_id' for resource '#{resource_el}'"
169
+ end
170
+ end
171
+ #puts "TYPE: #{type} - #{sliver_gurn}"
172
+ # opts = {
173
+ # :domain => sliver_gurn.domain,
174
+ # :type => resource_el.name # TODO: This most likely will break with NS
175
+ # }
176
+ # comp_gurn = OMF::SFA::Resource::GURN.create("#{sliver_gurn.short_name}:#{client_id}", opts)
177
+ comp_gurn = sliver_gurn
158
178
  if resource = OMF::SFA::Resource::OComponent.first(:urn => comp_gurn)
159
179
  context[client_id] = resource if client_id
160
- return resource.from_sfa(resource_el, context)
180
+ resource.from_sfa(resource_el, context, type)
181
+ resource.save
182
+ return
161
183
  end
162
184
  end
163
185
 
164
186
  # Appears the resource doesn't exist yet, let's see if we can create one
165
- type = comp_gurn.type
187
+ type = resource_el.name #comp_gurn.type
166
188
  if res_class = @@sfa_name2class[type]
167
- resource = res_class.new(:name => comp_gurn.short_name)
189
+ resource = res_class.new(:name => comp_gurn.short_name, :urn => comp_gurn)
190
+ #puts ">>> #{comp_gurn} - #{resource.to_hash}"
168
191
  context[client_id] = resource if client_id
169
- return resource.from_sfa(resource_el, context)
192
+ resource.from_sfa(resource_el, context, type)
193
+ #puts "22>>> #{resource.to_hash}"
194
+ return
170
195
  end
171
196
  raise "Unknown resource type '#{type}' (#{@@sfa_name2class.keys.join(', ')})"
172
197
  end
@@ -490,16 +515,20 @@ module OMF::SFA
490
515
  #
491
516
  # @param context Already defined resources in this context
492
517
  #
493
- def from_sfa(resource_el, context = {})
518
+ def from_sfa(resource_el, context = {}, type = 'manifest')
494
519
  els = {} # this doesn't work with generic namespaces
495
520
  resource_el.children.each do |el|
496
521
  next unless el.is_a? Nokogiri::XML::Element
497
522
  unless ns = el.namespace
498
523
  raise "Missing namespace declaration for '#{el}'"
499
524
  end
525
+ name = el.name
500
526
  unless ns.href == SFA_NAMESPACE_URI
501
- warn "WARNING: '#{el.name}' Can't handle non-default namespaces '#{ns.href}'"
502
- next
527
+ unless prefix = self.class._sfa_prefix_for_namespace(ns.href)
528
+ warn "#{resource_el.name}: Ignoring unknown element '#{el.name}' - NS: '#{ns.href}'"
529
+ next
530
+ end
531
+ name = "#{prefix}__#{name}"
503
532
  end
504
533
  (els[el.name] ||= []) << el
505
534
  end
@@ -1,4 +1,4 @@
1
1
  module OMF; module SFA
2
2
  #VERSION = "6.0.0.pre.1"
3
- VERSION = "0.2.2"
3
+ VERSION = "0.2.3"
4
4
  end; end
data/omf_sfa.gemspec CHANGED
@@ -24,6 +24,7 @@ Gem::Specification.new do |s|
24
24
  s.add_runtime_dependency "erector", "~> 0.8.3"
25
25
  s.add_runtime_dependency "rack", "~> 1.3.5"
26
26
  s.add_runtime_dependency "thin", "~> 1.3.1"
27
+ s.add_runtime_dependency 'thin_async'
27
28
  s.add_runtime_dependency "log4r", "~> 1.1.10"
28
29
  s.add_runtime_dependency "maruku", "~> 0.6.0"
29
30
  s.add_runtime_dependency "dm-core", "~> 1.2.0"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omf_sfa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-02-26 00:00:00.000000000 Z
12
+ date: 2014-03-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest
@@ -91,6 +91,22 @@ dependencies:
91
91
  - - ~>
92
92
  - !ruby/object:Gem::Version
93
93
  version: 1.3.1
94
+ - !ruby/object:Gem::Dependency
95
+ name: thin_async
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
94
110
  - !ruby/object:Gem::Dependency
95
111
  name: log4r
96
112
  requirement: !ruby/object:Gem::Requirement