B Sample Scripts for Using the Shared Data Area
This appendix provides sample scripts. These scripts follow the logic outlined in "An Example of Using the Shared Data Area".
Sample Python Module
The following script is supplied as a sample only and is not intended for deployment. Oracle Support does not provide support for this script.
When applied to the device driver using the command-line parameter -SharedDataModule, the following script defines the structure of the shared data area.
# This shared info module does not handle removing behavior objects # from lifetime shared memory. One way to deal with this issue is for # each behavior object to define an idea of identity and the shared # Module to insert behaviors in a structure that can be accessed by # identity so that specific objects can be removed. # Python "reflection" would be needed to help solve the problem in this # implementation. class SharedInfo: # In these functions get and set are methods to get data # from/insert data into the shared area functions with Add relate # to data to be treated as an add action(normally added by the # "on install" part of a data script functions, with Remove # relate to data to be treated as a remove action (normally added # by the "on remove" part of a data script def __init__(self): self.behaviors={} # Dictionary for behavior objects. self.data={"add":{},"remove":{}} # Dictionary for data.- broken into add and remove sections. self.addInterface={} # Dictionary for add data to interfaces. self.removeInterface={} # Dictionary for remove data to interfaces. self.general={} # Some simple functions to ensure that objects exist before being # accessed. def fetchDict(self, container, key): if( not container.has_key(key) ): container[key]={} return container[key] def fetch(self, container, key): if( not container.has_key(key) ): container[key]=[] return container[key] def insert(self, container, key, value ): if( not container.has_key(key) ): container[key]=[] container[key].append(value) # Insert the behavior object into the behaviors dictionary, keying # on the priority of the behavior object. def insertBehavior(self, behavior): self.insert( self.behaviors, behavior.priority(), behavior ) # Return all of the registered behavior objects def fetchBehavior(self): return self.behaviors #Insert data keyed by data type and interface name. def setInterfaceAddData(self, iface, name, data): self.insert( self.fetchDict( self.addInterface, name), iface, data) def setInterfaceRemoveData(self, iface, name, data): self.insert( self.fetchDict( self.removeInterface, name), iface, data) # Get data of type "name" for all interfaces def getInterfaceAddData(self, name): return self.fetchDict( self.addInterface, name) def getInterfaceRemoveData(self, iface, name): return self.fetchDict( self.removeInterface, name) # Get data of type "name" for this interface only. def getThisInterfaceAddData(self, iface, name): return self.fetch( self.fetchDict( self.addInterface, name), iface) def getThisInterfaceRemoveData(self, iface, name): return self.fetch( self.fetchDict( self.removeInterface, name), iface) # Data for a given key is stored as a list ( in case multiple # scripts register against the same key). "TrustMe" functions can # be used when the calling script is sure that there will # be an entry for the key, and that there is only one entry, # or the first entry is the only one it cares about. def getInterfaceAddDataTrustMe(self, iface, name): return self.fetch( self.fetchDict( self.addInterface, name), iface)[0] def getInterfaceRemoveDataTrustme(self, iface, name): return self.fetch( self.fetchDict( self.removeInterface, name), iface)[0] # Generic get and set data methods for add and remove def setAddData(self, name, data): self.insert( self.data["add"], name, data) def setRemoveData(self, name, data): self.insert( self.data["remove"], name, data) def getAddData(self, name): return self.fetch( self.data["add"], name) def getRemoveData(self, name): return self.fetch( self.data["remove"], name) def getAddDataTrustMe(self, name): return self.fetch( self.data["add"], name)[0] def getRemoveDataTrustme(self, name): return self.fetch( self.data["remove"], name)[0] # This function will be called by the driver whenever a new shared data # object is needed. It returns an object of the class type created # above. def CreateSharedObject(): return SharedInfo()
Sample Behavior Script
The following script is supplied as a sample only and is not intended for deployment. Oracle Support does not provide support for this script.
#Title: Sample behavior script Version = '1.0' #IP Service Activator version: 7.0.0 #Date: 19-Mar-2008 IOS level: 12.1 - 12.2.8 #(c) T.Romain 2008 #This script is a sample generic behavior script # #begin preamble section #N/A ###################################################################### #begin behavior section #script_name = "do X" #script_driver_type = "cisco" #script_type = Device #script_device_role = Access #script_interface_role = Any #script_apply_when = Before #script_repeat = False #script_apply_on_restart = True #begin common section #begin install section class DoXer: def __init__( self ): self.reportingData={"id":_id, "failureCode":_failure_code} # The priority to execute this with: the lowest number will be # executed first. def priority( self ): return 2 def Execute(self, parse): print "executing X script : priority "+ str( self.priority() ) # Retrieve data generated by pre-created data object interfaceData=_device.getThisCommitSharedData().getInterfaceAddData("XDATA") if( len(interfaceData) != 0 ): # we have some interfaces # Perform some function for iface in interfaceData.keys(): print "Requested to put X on " + iface else: print "No interfaces to do X to" # Finally insert an instance of the class into the shared area _device.getLifetimeSharedData().insertBehavior( DoXer() ) #begin remove section
Sample Data Script
The following script is supplied as a sample only and is not intended for deployment. Oracle Support does not provide support for this script.
#Title: Sample data script Version = '1.0' #IP Service Activator version: 7.0.0 #Date: 19-Apr-2008 IOS level: 12.1 - 12.2.8 #(c) T.Romain 2008 #This script is a sample generic data script # #begin preamble section #N/A #begin behavior section #script_name = "iface wants X" #script_driver_type = "cisco" #script_type = Interface #script_device_role = Access #script_interface_role = Any #script_apply_when = Before #script_repeat = False #script_apply_on_restart = True #begin common section data={ "_reporting_id":_id, "_failure_code":_failure_code, #begin install section _device.getThisCommitSharedData().setInterfaceAddData(_interface.getInterfaceName(), "XDATA", data) #begin remove section
Sample Controller Script
The following script is supplied as a sample only and is not intended for deployment. Oracle Support does not provide support for this script.
#Title: Sample controller script Version = '1.0' #IP Service Activator version: 7.0.0 #Date: 19-Apr-2008 IOS level: 12.1 - 12.2.8 #(c) T.Romain 2008 #This is a sample controller script # #begin preamble section #N/A ###################################################################### #begin behavior section #script_name = "controller" #script_driver_type = "cisco" #script_type = Device #script_device_role = Access #script_interface_role = Any #script_apply_when = After #script_repeat = True #script_apply_on_restart = True #begin common section #begin install section import string import sre tempbehaviors =_device.getThisCommitSharedData().fetchBehavior() permbehaviors =_device.getLifetimeSharedData().fetchBehavior() _device.openSession() config= _device.deliverCommand("show running-config") splitConfig=string.split(config,"\n") _device.closeSession() tmp=tempbehaviors.keys() + permbehaviors.keys() tmp.sort() lastI=-1 for i in tmp : if( lastI!= i ): lastI=i if( permbehaviors.has_key(i) ): for j in permbehaviors[i]: print j result=j.Execute( splitConfig ) if( result): print "FAILED!" #_result.sendScriptObjectFailure(result[0],result[1],result[2]) if( tempbehaviors.has_key(i) ): for j in tempbehaviors[i]: print j result=j.Execute( splitConfig ) if( result): print "FAILED" #begin remove section