Sun Open Telecommunications Platform 2.0 Developer's Guide

Calling N1 SPS Plans From C/C++

Since there is no N1 SPS C/C++ interface available, you can invoke the plans using N1 SPS CLI. Two N1 SPS CLI invocations are required.

For detailed information about N1 SPS CLI, see Sun N1 Service Provisioning System 5.2 Command-Line Interface Reference Manual.

The following sample code illustrates how to run a plan from the C++ code.

/*
 * Example of running an N1SPS plan from C++ code.
 *
 * To simplify this example, not all possible errors have been handled
 * properly.
 */

///////////////////////////////////////////////////////////////////////////////

#include<iostream>
#include<sstream>
#include<cstdlib>

///////////////////////////////////////////////////////////////////////////////

//path to N1SPS CLI
#define N1SPS_CLI "/var/js/spsotp/N1_Service_Provisioning_System_5.2/cli/bin/cr_cli"

//plan to run
#define PLAN "/com/sun/OTP/security/plans/InstallSecurity"

//variable setting version string - see N1SPS CLI doc for more info
#define PLAN_COMP_DEFAULT_VS "+;+;+,+,+,+,+,+,+,+"

//component version string - N1SPS CLI doc for more info
#define DEFAULT_COMP DEFAULT_VS

//timeout in seconds - see N1SPS CLI doc (pto and nto parameters) for details
#define TIMEOUT_SEC 600

#define BUFF_SIZE 100

///////////////////////////////////////////////////////////////////////////////

using namespace std;

/**
 * prints usage message
 */
void usage() {
	cerr << "Usage:" << endl
		 << "listTasks <login> <password> <target>" << endl;
}

///////////////////////////////////////////////////////////////////////////////

/**
 * main function
 * expected argv content: login, password, target
 */
int main(int argc, char **argv) {

	if( argc != 4 ) {
		//incorrect number of parameters, so print usage information
		usage();
	} else {
		cout << "Running plan " << PLAN << ":" << endl << endl;

		//prepare string for invocation of N1SPS CLI to run a plan
		stringstream s;
		s << N1SPS_CLI << " -cmd pe.p.run -u " << argv[1] << " -p " << argv[2]
		  << " -tar H:NM:" << argv[3] << " -PID NM:" << PLAN
		  << " -comp " << DEFAULT_COMP << " -vs " << DEFAULT_VS
		  << " -pto " << TIMEOUT_SEC << " -nto " << TIMEOUT_SEC;

		FILE *f = NULL;
		if( (f = popen(s.str().c_str(), "r")) == NULL ) {
			//popen failed (fork() or pipe() failed), so print error message
			cerr << "Error running plan " << PLAN << ": popen() failed" << endl;
		} else {
			//read N1SPS CLI output (i.e. Task ID of running plan)
			char buff[BUFF_SIZE];
			fgets(buff, BUFF_SIZE, f);

			//wait for N1SPS CLI to finish
			if( pclose(f) != 0 ) {
				cerr << "Error running plan " << PLAN << ": N1SPS CLI failed" << endl;
				return -1;
			}

			cout << "Plan " << PLAN << "execution started." << endl
				 << "Task ID:" << buff << endl;

			/*
			 * prepare a new string for N1SPS CLI invocation to check on progress
			 * of the running task
			 */
			s.str("");
			s << N1SPS_CLI << " -cmd pe.p.lo -u " << argv[1] << " -p " << argv[2]
			  << " -ID " << buff;

			cout << "Checking progress of task ID " << buff << endl; 

			/*
			 * wait for the running task to finish
			 *
			 * return the same return code N1SPS CLI returns
			 */
			return system(s.str().c_str());
		}
	}
	return -1;
}

To get a code in plain C, replace all stream related C++ specific code to equivalent code in C.