Extending Service Implementations with Script Services
You can extend SAE-managed services to provision policies on a number of systems across a network, including networks that do not contain a JUNOSe router or JUNOS routing platform. Script services are value-added services that provide an interface to call scripts that supply custom services. You can use script services to create custom service implementations, such as:
- Provisioning of layer 2 devices, such as digital subscriber line access multiplexers (DSLAMs).
- Setting up of network connections such as MPLS tunnels.
- Provisioning of policies for network devices that do not have a supported SAE router driver.
To customize service implementations:
- Write a script that implements the ScriptService interface, a service provider interface (SPI) for the SAE.
- Add a script service that references the script.
Writing Scripts for Script Services
The ScriptService SPI provides a Java interface that a script service implements. For information about the ScriptService interface and the ServiceSessionInfo interface, see the script service documentation in the SRC software distribution in the folder SDK/doc/sae or in the SAE core API documentation on the Juniper Networks Web site at
http://www.juniper.net/techpubs/software/management/sdx/api-index.html
The implementation of the ScriptService interface activates the service. The SAE sends authentication and tracking events when it activates, modifies, or deactivates a script service session.
The SAE supports script services written in Java or Jython. For scripts written in Java, you must compile and package the implemented ScriptService to make it available for use by the SAE. A Java implementation can include more than one Java archive (JAR) file.
The SAE synchronizes methods used by the same instance of the ScriptService class. You do not need to provide synchronized implementation of the methods.
To write a script to be used by a script service:
- Create a class that provides a default constructor and that implements the ScriptService interface.
- Manage activation and manipulation of the service session by implementing the following ScriptService methods:
- activateSession()—Activates the script service session.
- deactivateSession()—Deactivates the script service session and returns any final accounting data for the script service session.
- modifySession()—If the counters were reset during the modification, modifies the script service session and returns any accounting data.
These methods are passive; that is, they perform the associated action (activate, deactivate, modify) when the SAE calls the method.
- (Optional) Get information about service sessions by using methods on the ServiceSessionInfo interface.
- (Optional) Provide accounting data, if used, by using the following ScriptService method:
getAccountingData()—Polls for current accounting data and returns any current accounting data.
getState()—Returns session data to be stored persistently on the router. The SAE does not use this data but provides it to the script when a service session is restored after failover.
- initState() —Initializes a recovered script service session after a state synchronization.
- discarded()—Provides notification that the service session has been discarded. Service sessions are discarded when the SAE loses connection to a router. A discarded service session continues to exist on the router and is restored after the connection to the router is reestablished by an SAE.
The script service session releases any resources associated with a discarded session, but must not take any action to disrupt the service session.
You can also use the stopService() method on the ServiceSessionInfo object to stop a service and remove the service from the SAE. For example, in a script service that monitors a state that it creates outside the SAE, if the script detects that the service is not active, it can stop the service and remove it from SAE. You could use this type of script service to start a daemon process and monitor the process to make sure that it is alive.
NOTE: The ScriptService SPI does not provide access to a router driver.
Example: ScriptService SPI in Jython
The following example implements the ScriptService SPI in Jython.
from net.juniper.smgt.sae.scriptservice import ScriptServiceclass SampleService(ScriptService):def initSessionInfo(self, ssi):self.ssi = ssidef activateSession(self):print "Activating ServiceName %s" % self.ssi.serviceNamedef deactivateSession(self):print "Deactivating ServiceName %s" % self.ssi.serviceNamereturn Nonedef modifySession(self, ssi):self.ssi = ssiprint "Modifying ServiceName %s" % self.ssi.serviceNamereturn Nonedef getAccountingData(self):print "Getting accounting data for ServiceName %s" % self.ssi.serviceNamereturn Nonedef getState(self):return Nonedef initState(self, ssi, state):self.ssi = ssipassdef discarded(self):passExample: ScriptService SPI in Java
The following example implements the ScriptService SPI in Java.
class SampleService implements ScriptService {private ServiceSessionInfo ssi;public SampleService() { }public void initSessionInfo(ServiceSessionInfo ssi) {this.ssi = ssi;}public void activateSession() {System.out.println("Activating ServiceName "+ssi.getServiceName());}public AccountingData deactivateSession() {System.out.println("Deactivating ServiceName "+ssi.getServiceName());return null;}public AccountingData modifySessionSession(ServiceSessionInfo ssi) {this.ssi = ssi;System.out.println("Modifying ServiceName "+ssi.getServiceName());return null;}public AccountingData getAccountingData() {System.out.println("Getting accounting data for ServiceName "+ssi.getServiceName());return null;}public byte[] getState() {return null;}public initState(ServiceSessionInfo ssi, byte[] state) {this.ssi = ssi;}public void discarded() {}}Adding Script Services
Before you add a script service, make sure that you know the location of the script file that the service will reference.
- In the SDX Admin navigation pane, right-click the Services folder, highlight New, and then click SSP service.
- Enter a name for the service, and click OK.
- In the Main tab pane, select Script in the Type field.
![]()
- Click the Script tab.
The Script tab appears in the content pane.
![]()
- Using the field descriptions in Configuring Values for Script Services, configure the Script Type and Class Name.
- If the script type is URL, enter the URL in the File/URL box.
If the script type is not URL, click Load.
The Load data dialog box appears.
![]()
- Select the directory that holds the script that contains the implementation of the ScriptService interface; then select the file. Or type the path to the script file in the Selection box, and click OK.
If a JAVA implementation includes more than one JAR file, use commas to separate file URL entries or enter one URL per line.
The content of the script file appears in the File/URL box in the Script pane.
You can manipulate files and folders from the Load data dialog box.
The Rename File dialog box appears.
Configuring Values for Script Services
Use the following field descriptions to provide information about the script to be used by the script service.
Script Type
- URL—URL to identify the location of script file
- Python—Jython source code
- Java Class—Compiled Java class file
- Java Archive—Java archive file (.jar)
Class Name
- Name of the class that implements the ScriptService SPI. The SAE instantiates this name when it starts the script service.
- Value—Name of the class
- Default—No value
File/URL
To add a script, see Writing Scripts for Script Services.
Removing a File or URL from a Script Service