SDN Controller Programming Guide
159
@Reference(policy = ReferencePolicy.DYNAMIC,
cardinality = ReferenceCardinality.MANDATORY_UNARY)
private volatile ServiceRest restClient;
@Reference(policy = ReferencePolicy.DYNAMIC,
cardinality = ReferenceCardinality.MANDATORY_UNARY)
private volatile jsonService jsonService;
@Override
public Set<Switch> getControlledDevices(IpAddress ipAddress) {
URI uri = restClient.uri(ipAddress, BASE_DESTINATION_PATH);
ResponseData response = restClient.get(restClient.login(), uri);
String responseData;
try {
responseData = new String(response.data(), StringUtils.UTF8);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(
"Unable to decode response from " + ipAddress, e);
}
if (response.status() != Status.OK.getStatusCode()) {
StringBuilder message = new StringBuilder(32);
message.append("Unable to communicate with ");
message.append(ipAddress);
message.append(". Status code: ");
message.append(response.status());
message.append(". Response data: ");
message.append(responseData);
throw new RuntimeException(message.toString());
}
List<Switch> remoteDevices = jsonService.fromJsonList(
responseData, Switch.class);
return new HashSet<Switch>(remoteDevices);
}
}
ServiceRest is a service provided by the HP VAN SDN Controller framework that enables HTTP
communication by offering the common operations GET, POST, PUT and DELETE. It also takes care
of service authentication. In order to use from ServiceRest we need to add the module it is located
at as a dependency. Open the hm-bl/pom.xml file and add the XML extract from the following
listing, ServiceRest Dependency, to the <dependencies> node; after updating the POM file update
the Eclipse project dependencies (see Updating Project Dependencies on page 115).