SDN Controller Programming Guide
155
@Deactivate
protected void deactivate() {
clearCodecs();
}
}
HmJsonFactory holds all the JSON codecs; it is an OSGi service so it is registered to the central
JSON repository when it is activated and unregistered from the JSON repository when it is
deactivated. The registration happens automatically because the HP VAN SDN Controller
Framework observes all activated JSON Factories (JsonFactory) services annotated with the
following property: “name=flare”.
Now that the JSON factory is in place, update the SwitchResource to use the JsonService to
encode and decode Switch objects. The SwitchResource.java Using JSON Codecs listing shows a
modification of SwitchResource that uses a JSON codec to encode Switch objects.
SwitchResource.java Using JSON Codecs:
package com.hp.hm.rs;
import com.hp.sdn.json.JsonService;
...
@Path("switches")
public class SwitchResource extends ControllerResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response list() {
SwitchService service = get(SwitchService.class);
List<Switch> switches = service.find(null, null);
JsonService jsonService = get(JsonService.class);
String result = jsonService.toJsonList(switches, Switch.class, true);
return ok(result).build();
}
...
}
JsonService Module Dependencies:
<dependency>
<groupId>com.hp.sdn</groupId>
<artifactId>sdn-cmmon-api</artifactId>
<version>${sdn.version}</version>
</dependency>
The following listing, SwitchResourceTest.java Using JSON Codecs, is a modification of the
SwitchResourceTest.java that also uses JsonService to complete the tests.
SwitchResourceTest.java Using JSON Codecs:
package com.hp.hm.rs;