Have questions? Stuck? Please check our FAQ for some common questions and answers.

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

This tutorial shows you how to export services from your application, and to add a custom command to the ONOS CLI that uses the service. We will build on the reactive forwarding application from the Application tutorial.

Offering services to other modules

If you want your module to be able to provide services to other modules, then you should define a service interface and have your module class implement it - in our case this is the SdnIpService.

We start defining a new interface for the service:

SndIpService.java
 /**
 * Service interface exported by SDN-IP.
 */
public interface SdnIpService {
    /**
     * Gets the BGP sessions.
     *
     * @return the BGP sessions
     */
    public Collection<BgpSession> getBgpSessions();
}

 

The SdnIp class should implement this interface and export that service through the Felix SCR annotation @Service:

SdnIp.java
@Component(immediate = true)
@Service
public class SdnIp implements SdnIpService {
/**
* Component for the SDN-IP peering application.
*/
@Component(immediate = true)
public class SdnIp {
	private static final String SDN_IP_APP = "org.onlab.onos.sdnip";
	private final Logger log = getLogger(getClass());
	@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
	protected CoreService coreService;
 
	@Activate
	protected void activate() {
	log.debug("Started");
	ApplicationId appId = coreService.registerApplication(SDN_IP_APP);
	}
	@Deactivate
	protected void deactivate() {
	log.info("Stopped");
	} 
 
    @Override

    public Collection<BgpSession> getBgpSessions() {
        return bgpSessionManager.getBgpSessions();
    }
} 

 

At this point the service can be used in another class importing it through the Felix SCR annotation:

Felix annotation reference
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected SdnIpService SdnIpService;
  • No labels