SDN Controller Programming Guide

127
be declarative, such as using some sort of composition language to describe the components and
bindings among them.
By using components applications can be created easily and quickly by snapping them together
from readily available, reusable components. Components promote separation of concerns and
encapsulation with its interface based approach. This enhances the reusability of your code
because it limits dependencies on implementation details. Another worthwhile aspect of an
interface-based approach is substitutability of providers. Because component interaction occurs
through well-defined interfaces, the semantics of these interfaces must themselves be well defined.
As such, it’s possible to create different implementations and easily substitute one provider with
another.
The type of component model defined by OSGi is called service-oriented component model which
rely on execution-time binding of provided services to required services using the service-oriented
interaction pattern [34].
Continuing with the example, the SwitchService from the Service API on page 124 will be
published via SwitchManager from the Service Implementation on page 125 so it is available to
be consumed by other components.
SwitchManager is a Java object not bound by any restriction other than the service interface it
implements and those forced by the Java Language Specification; similar to a POJO [20]. Since
OSGi declarative services require a component to be annotated and to implement some methods
to bind/unbind other dependency components, a proxy component will be introduced (that
follows the proxy pattern [35]) to deal with OSGi allowing the business logic to be separated from
the OSGi restrictions. The following listing shows the OSGi service component used to publish
SwitchService via OSGi declarative services. The implementation of the OSGi component is also
located in the hm-bl module.
NOTE
The usage of
SwitchComponent
may be omitted and directly annotate
SwitchManager
if preferred.
SwitchComponent.java (Sample Application OSGi Service Component):
package com.hp.hm.impl;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
...
@Component
@Service
public class SwitchComponent implements SwitchService {
private SwitchService delegate;
public SwitchComponent() {
delegate = new SwitchManager();
}