SDN Controller Programming Guide
149
137). The web container manages the life-cycle of the Jersey Servlet (as illustrated in Figure 3); the
Jersey Servlet is defined at hm-rs/src/main/webapp/WEB-INF/web.xml.
Therefore, it is not possible to have OSGi injecting Domain Services into RESTful Web Services
because their life-cycle is managed by different technologies: OSGi and Servlets respectively. In
order to overcome this restriction and allow RESTful Web Services delegating to Domain Services
the HP VAN SDN Controller Framework provides a Domain-Service Repository (ServiceLocator)
that follows the Singleton Pattern [35]. However, it is necessary to write an OSGi compliant service
that subscribes/unsubscribes our Domain Services to/from the repository. Create the
ServiceAssistant lass shown in the following listing under hm-rs module.
ServiceAssistant.java:
package com.hp.hm.rs;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.References;
import com.hp.hm.api.SwitchService;
import com.hp.sdn.rs.misc.ServiceLocator;
...
@Component(immediate=true, specVersion="1.1")
@References(
value={
// Add a @Reference (Separated by comma) for each
// domain service exposed to the REST layer.
@Reference(name="SwitchService",
referenceInterface = SwitchService.class,
policy=ReferencePolicy.DYNAMIC,
cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE
)
}
)
public class ServiceAssistant {
// Add a bind/unbind methods for each Domain Service
// exposed to the REST layer.
protected void bindSwitchService(SwitchService service,
Map<String, Object> properties) {
ServiceLocator.INSTANCE.register(SwitchService.class,
service, properties);
}
protected void unbindSwitchService(SwitchService service) {
ServiceLocator.INSTANCE.unregister(SwitchService.class, service);
}