Reference Guide

Table Of Contents
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.1.4</version>
</dependency>
Domain Service - REST API Integration
Figure 3 illustrates a common pattern used when working with Servlets: The Model-View-Controller
(MVC) pattern. In this pattern the Servlet acts as the Controller. As mentioned before, when using
RESTful Web Services we don’t directly write Servlets, however a REST API acts as the Controller as
well. A normal behavior of a REST API includes:
1. Decode the requestJSON [36] format in our sample application.
2. Call domain services(Business Logic) to service the request.
3. Encode the result to include in the responseJSON [36] format in our sample application.
This section describes how to integrate the domain service (Business Logic) and the REST API
(RESTful web services). The objective is to have the REST layer delegating business logic to domain
services.
The life-cycle of Domain Services and RESTful web services [2] is managed by different
technologies. Domain serviceslife-cycle is managed by OSGi; we don’t need to create instances
of our domain services, OSGi will create them for us by scanning classes annotated with
@Component, and they will be ready to be consumed if they are annotated with @Service (as
illustrated in SwitchComponent.javaSample Application OSGi Service Componentfor more
information see Providing Services with OSGi Declarative Services on page 159). In the other
hand, RESTful web services are based on Servlets; Jersey Servlet [2] manages the life-cycle of the
REST APIs. Similarly to Domain Services, we don’t need to create instances of our REST APIs, the
Jersey Servlet will create them for us by scanning classes annotated with @Path; the Jersey Servlet
handles HTTP requests and dispatches to the right REST API based on the @Path annotations (as
illustrated in Creating Domain Service Resource (REST Interface of Business Logic Service) on page
169). 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 [34]. However, it is necessary to write an OSGi compliant service
that subscribes/unsubscribes our Domain Services to/from the repository. Create the
ServiceAssistant class shown in the following listing under hm-rs module.
ServiceAssistant.java:
package com.hp.hm.rs;
import org.apache.felix.scr.annotations.Component;
180