Class: SpecTools::Model

Inherits:
Object
  • Object
show all
Extended by:
SpecToolsExtensions
Includes:
SpecToolsExtensions
Defined in:
lib/spectools.rb,
lib/vnmsh.rb

Overview

SpecTools::Model

Represents a Spectrum model

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SpecToolsExtensions

call_default_extension, call_extension, method_missing, method_missing

Constructor Details

#initialize(handle = nil, name = nil, type = MType.new, landscape = Landscape.new, *attrs) ⇒ Model

Returns a new instance of Model.



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/spectools.rb', line 353

def initialize(handle = nil, name = nil, type = MType.new, landscape = Landscape.new,*attrs)
	if handle.nil? || handle.hex?
		@handle = handle
	else
		raise ArgumentError, "Handle is not a hex code"
	end
	@name = name
	@type = type
	@attrs = Hash.new
	@landscape = landscape
	attrs.each do |specattr|
		if specattr.nil? || specattr.instance_of?(Attr)
			@attrs[specattr.id] = specattr
		else
			raise ArgumentError
		end
	end
		
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class SpecToolsExtensions

Instance Attribute Details

#attrsObject

A hash of Attr objects for the model



349
350
351
# File 'lib/spectools.rb', line 349

def attrs
  @attrs
end

#handleObject

The model’s handle



343
344
345
# File 'lib/spectools.rb', line 343

def handle
  @handle
end

#landscapeObject

The landscape on which the Model exists.



351
352
353
# File 'lib/spectools.rb', line 351

def landscape
  @landscape
end

#nameObject

The model’s name



345
346
347
# File 'lib/spectools.rb', line 345

def name
  @name
end

#typeObject

The model’s MType



347
348
349
# File 'lib/spectools.rb', line 347

def type
  @type
end

Class Method Details

.cli_create(mtype, attrs = nil, lh = nil, session = nil) ⇒ Object

Use CLI to create a new model. mtype may either be a valid MType object, or a model type handle. attrs is an array of Attr objects that you want to be set on the model when it is created.



483
484
485
486
# File 'lib/vnmsh.rb', line 483

def self.cli_create(mtype,attrs=nil,lh=nil,session=nil)
	session = VNMSH.get_session(session)
	return session.create_model(mtype,attrs,lh)
end

.cli_destroy(model, session = nil) ⇒ Object

model can be either a valid Model, or a model handle.



454
455
456
457
# File 'lib/vnmsh.rb', line 454

def self.cli_destroy(model,session=nil)
	session = VNMSH.get_session(session)
	session.destroy_model(model)
end

.cli_discover(ip, community = nil, timeout = nil, retries = nil, sdm = nil, landscape = nil, session = nil) ⇒ Object

Use CLI to create a new model by IP.

Options:

  • ip - Required. The device’s IP address.

  • community - Optional. The community string.

  • timeout - Optional. The device’s timeout.

  • retries - Optional. The number of times to attempt to contact the device.

  • sdm - Optional. The address of the Secure Domain Connector.

  • landscape - Optional. The landscape to creat the device on.



474
475
476
477
# File 'lib/vnmsh.rb', line 474

def self.cli_discover(ip,community=nil,timeout=nil,retries=nil,sdm=nil,landscape=nil,session=nil)
	session = VNMSH.get_session(session)
	session.discover_device(ip,community,timeout,retries,sdm,landscape)
end

.cli_find(filter = nil, devices = false, session = nil) ⇒ Object

Use CLI to locate Models. Returns an array of Model objects.

filter is a hash of filter types and thier values.

If no filter is passed, returns all Models for the connected Landscape.

If devices is true, returns all devices. In this case, filter is ignored.

Filter options:

  • :range - filters on a range of models.

  • Range can either be a single string, an Array of handles, or an Array of Models.

  • Example: Model.cli_find({:range,'0x400000-0x500000'})

  • Example: Model.cli_find(:range,['0x400000','0x500000']})

  • :name - filters on the model’s name (partials accepted)

  • Example: Model.cli_find({:name,'VNM'})

  • :landscape - filters on the given Landscape.

  • Example: Model.cli_find({:landscape,'0x400000'})

  • :mtype - Returns all models of the specified type.

  • Type can either be an MType object, or a model type handle.

  • Examples:

    • Model.cli_find(:mtype, '0x13d0018')

    • Model.cli_find(:mtype, MType.new('0x13d0018'))



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
# File 'lib/vnmsh.rb', line 302

def self.cli_find(filter=nil,devices=false,session=nil)
	session = VNMSH.get_session(session)
	models = Array.new
	if devices
		if filter && filter[:landscape]
			model_output = session.show_devices(filter[:landscape])
		else
			model_output = session.show_devices
		end
	else	
		model_output = session.show_models(filter)
	end
	model_output.each do |line|
		model = Model.parse(line)
		if filter && filter[:landscape]
			if filter[:landscape].kind_of?(Landscape)
				model.landscape = filter[:landscape]
			else
				model.landscape = Landscape.new(filter[:landscape])
			end
		else
			model.landscape = session.current_landscape
		end
		models.push(model)
	end
	return models
end

.cli_find_by_attr(specattr, value = nil, substring = false, nocase = false, session = nil) ⇒ Object

Use CLI to locate Models by attribute.

specattr may be an Attr object or an attribute ID. If specattr is an Attr and has the value attribute set, value does not have to be set. However, you can override the Attr object’s value in the search by providing your own value.

If specattr.value is nil, or of specattr is simply an attribute ID, value must be set.

If substring is true, a partial string match is performed.

If nocase is true, case is ignored.



345
346
347
348
349
350
351
352
353
# File 'lib/vnmsh.rb', line 345

def self.cli_find_by_attr(specattr,value=nil,substring=false,nocase=false,session=nil)
	session = VNMSH.get_session(session)
	models = Array.new
	model_output = session.seek(specattr,value,substring,nocase)
	model_output.each do |line|
		models.push(Model.parse(line))
	end
	return models
end

.cli_parse(line) ⇒ Object

Take a line of CLI show models or seek output and populate a new Model object.



269
270
271
272
273
274
# File 'lib/vnmsh.rb', line 269

def self.cli_parse(line)
	mhandle,mname,mthandle,mtname = line.chomp.unpack('A12A1025A12A20')
	mtype = MType.new(mthandle,mtname)
	model = Model.new(mhandle,mname,mtype)
	return model
end

Instance Method Details

#cli_associate(position, model, relation, session = nil) ⇒ Object

Use CLI to associate this model with another. position is either :left or :right, and indicates which side of this assocaition the current model will reside. The model argument may be a valid Model or a model handle.

  • Example: left_model.associate(:left,right_model,relation)



510
511
512
513
514
515
516
517
518
519
# File 'lib/vnmsh.rb', line 510

def cli_associate(position,model,relation,session = nil)
	session = VNMSH.get_session(session)
	if position == :left
		return session.create_association(relation,self,model)
	elsif position == :right
		return session.create_association(relation,model,self)
	else
		raise ArgumentError, "Valid options are :left or :right"
	end
end

#cli_destroy(session = nil) ⇒ Object

Use CLI to destroy this model.



460
461
462
# File 'lib/vnmsh.rb', line 460

def cli_destroy(session=nil)
	self.class.destroy(self.handle,session)
end

#cli_get_attrs(filter = nil, enumerate = false, session = nil) ⇒ Object

Use CLI to retrieve a hash of Attr objects for a given Model.

filter is a hash of filter types and thier values.

If no filter is given, all attributes are returned. If enumerate is set to true, the values will be automatically enumerated.

Filter options.

  • :id - returns the specific attribute.

  • Takes an Attr or an attribute ID.

  • Returns all instances for a list attribute.

  • Example: model.cli_get_attrs({:id,'0x10000'})

  • :instance = returns a specific instance of an attribute.

  • Takes a 2 element array.

    • The first element must be an Attr or an attribute id.

    • The second element must be an instance identifier.

  • Example: model.cli_get_attrs({:instance,['0x10000','192.168.1.1']})

  • :range - returns attributes whose ID in in the specified range.

  • Range can take a single range string, or an array of Model handles or Models.

  • Example: model.cli_get_attrs({:range,['0x10000','0x1000a])

  • Example: model.cli_get_attrs({:range,'0x10000-0x1000a'})

  • :name - returns attributes whose name contains the specified string.

  • Example: model.cli_get_attrs({:name,'Condition'})



380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/vnmsh.rb', line 380

def cli_get_attrs(filter=nil,enumerate=false,session=nil)
	unless self.handle.hex?
		raise ArgumentError, 'Must supply a valid model handle'
	end
	attrs = Hash.new
	session = VNMSH.get_session(session)

	attr_output = session.show_attributes(:model,self,filter,enumerate)
	attr_output.each do |line|
		tmpattr = Attr.parse_mh(line)
		if attrs[tmpattr.id] && tmpattr.list && attrs[tmpattr.id].value_table
			newiid = tmpattr.value_table.keys.first
			attrs[tmpattr.id].value_table[newiid] = 
				tmpattr.value_table[newiid]
		else
			attrs[tmpattr.id] = tmpattr
		end
	end
	return attrs
end

#cli_get_attrs!(filter = nil, enumerate = false, session = nil) ⇒ Object

Call cli_get_attrs and populate the current Model’s attrs attribute. Takes the same arguments as cli_get_attrs



447
448
449
450
# File 'lib/vnmsh.rb', line 447

def cli_get_attrs!(filter=nil,enumerate=false,session=nil)
	attrs = self.cli_get_attrs(filter,enumerate,session)
	self.attrs = attrs
end

#cli_get_children(relation = nil, session = nil) ⇒ Object

Use CLI to retreive the children of the current model. Returns an array of Model objects.

If relation is set to a Relation or a relation name, only parents with the given relation will be retrieved.



407
408
409
410
411
412
413
414
415
# File 'lib/vnmsh.rb', line 407

def cli_get_children(relation=nil,session=nil)
	models = Array.new
	session = VNMSH.get_session(session)
	model_output = session.show_children(self,relation)
	model_output.each do |line|
		models.push(Model.parse(line))
	end
	return models
end

#cli_get_parents(relation = nil, session = nil) ⇒ Object

Use CLI to retreive the parents of the current model. Returns an array of Model objects.

If relation is set to a Relation or a relation name, only parents with the given relation will be retrieved.



423
424
425
426
427
428
429
430
431
# File 'lib/vnmsh.rb', line 423

def cli_get_parents(relation=nil,session=nil)
	models = Array.new
	session = VNMSH.get_session(session)
	model_output = session.show_parents(self,relation)
	model_output.each do |line|
		models.push(Model.parse(line))
	end
	return models
end

#cli_get_watches(session = nil) ⇒ Object

Use CLI to retreive the watches of the current model. Returns an array of Watch objects.



435
436
437
438
439
440
441
442
443
# File 'lib/vnmsh.rb', line 435

def cli_get_watches(session=nil)
	watches = Array.new
	session = VNMSH.get_session(session)
	watch_output = session.show_watch(self)
	watch_output.each do |line|
		watches.push(SpecTools::Watch.parse(line))
	end
	return watches
end

#cli_update_attrs(attrs, update_model = false, session = nil) ⇒ Object

Use CLI to update a model’s attributes.

attrs can be a single Attr object, or an array of Attr objects.

If update_model is true , the attrs of the model will be updated, as well.



493
494
495
496
497
498
499
500
501
502
# File 'lib/vnmsh.rb', line 493

def cli_update_attrs(attrs,update_model = false,session=nil)
	session = VNMSH.get_session(session)
	result = session.update_model(self,attrs)
	if result && update_model
		attrs.each do |specattr|
			self.attrs[specattr.id] = specattr
		end
	end
	return result
end