Ray Milkey

Cleanups to REST code suggested by Sonar

Change-Id: Ia399da70e7cd140514e07b63d9b2965fe86bbce7
......@@ -142,21 +142,21 @@ public class FlowsWebResource extends AbstractWebResource {
InputStream stream) {
URI location;
try {
FlowRuleService service = get(FlowRuleService.class);
ObjectNode root = (ObjectNode) mapper().readTree(stream);
JsonNode specifiedDeviceId = root.get("deviceId");
ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
JsonNode specifiedDeviceId = jsonTree.get("deviceId");
if (specifiedDeviceId != null &&
!specifiedDeviceId.asText().equals(deviceId)) {
throw new IllegalArgumentException(
"Invalid deviceId in flow creation request");
}
root.put("deviceId", deviceId);
FlowRule rule = codec(FlowRule.class).decode(root, this);
jsonTree.put("deviceId", deviceId);
FlowRule rule = codec(FlowRule.class).decode(jsonTree, this);
service.applyFlowRules(rule);
location = new URI(Long.toString(rule.id().value()));
} catch (IOException | URISyntaxException ex) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
throw new IllegalArgumentException(ex);
}
return Response
.created(location)
.build();
......
......@@ -125,7 +125,7 @@ public class HostsWebResource extends AbstractWebResource {
hostProviderRegistry.unregister(hostProvider);
} catch (IOException ex) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
throw new IllegalArgumentException(ex);
}
return Response
.created(location)
......@@ -137,19 +137,20 @@ public class HostsWebResource extends AbstractWebResource {
new ProviderId("host", "org.onosproject.rest", true);
private HostProviderService hostProviderService;
// Not implemented since there is no need to check for hosts on network
public void triggerProbe(Host host) {
private InternalHostProvider() {
}
// Creates new InternalHostProvider with a HostProviderRegistry param.
private InternalHostProvider() {
public void triggerProbe(Host host) {
// Not implemented since there is no need to check for hosts on network
}
public void setHostProviderService(HostProviderService service) {
this.hostProviderService = service;
}
// Return the ProviderId of "this"
/*
* Return the ProviderId of "this"
*/
public ProviderId id() {
return providerId;
}
......@@ -161,7 +162,7 @@ public class HostsWebResource extends AbstractWebResource {
*/
private HostId parseHost(JsonNode node) {
MacAddress mac = MacAddress.valueOf(node.get("mac").asText());
VlanId vlanId = VlanId.vlanId(((short) node.get("vlan").asInt((VlanId.UNTAGGED))));
VlanId vlanId = VlanId.vlanId((short) node.get("vlan").asInt(VlanId.UNTAGGED));
JsonNode locationNode = node.get("location");
String deviceAndPort = locationNode.get("elementId").asText() + "/" +
locationNode.get("port").asText();
......
......@@ -35,7 +35,11 @@ import static org.onosproject.net.PortNumber.portNumber;
@Path("links")
public class LinksWebResource extends AbstractWebResource {
enum Direction { ALL, INGRESS, EGRESS }
enum Direction {
ALL,
INGRESS,
EGRESS
}
@GET
public Response getLinks(@QueryParam("device") String deviceId,
......
......@@ -280,13 +280,11 @@ public class NetworkConfigWebResource extends AbstractWebResource {
@SuppressWarnings("unchecked")
public Response delete() {
NetworkConfigService service = get(NetworkConfigService.class);
service.getSubjectClasses().forEach(subjectClass -> {
service.getSubjects(subjectClass).forEach(subject -> {
service.getConfigs(subject).forEach(config -> {
service.removeConfig(subject, config.getClass());
});
});
});
service.getSubjectClasses()
.forEach(subjectClass -> service.getSubjects(subjectClass)
.forEach(subject -> service.getConfigs(subject)
.forEach(config -> service
.removeConfig(subject, config.getClass()))));
return Response.ok().build();
}
......@@ -303,11 +301,10 @@ public class NetworkConfigWebResource extends AbstractWebResource {
@SuppressWarnings("unchecked")
public Response delete(@PathParam("subjectKey") String subjectKey) {
NetworkConfigService service = get(NetworkConfigService.class);
service.getSubjects(service.getSubjectFactory(subjectKey).getClass()).forEach(subject -> {
service.getConfigs(subject).forEach(config -> {
service.removeConfig(subject, config.getClass());
});
});
service.getSubjects(service.getSubjectFactory(subjectKey).getClass())
.forEach(subject -> service.getConfigs(subject)
.forEach(config -> service
.removeConfig(subject, config.getClass())));
return Response.ok().build();
}
}
......
......@@ -38,25 +38,16 @@ import org.onosproject.rest.AbstractWebResource;
@Path("paths")
public class PathsWebResource extends AbstractWebResource {
// Host id format is 00:00:00:00:00:01/-1
private static final int VLAN_SEPARATOR_OFFSET = 17;
private static final int FIRST_MAC_ADDRESS_SEPARATOR_OFFSET = 2;
private static final int SECOND_MAC_ADDRESS_SEPARATOR_OFFSET = 5;
private static final int THIRD_MAC_ADDRESS_SEPARATOR_OFFSET = 8;
/**
* Determines if the id appears to be the id of a host.
* Host id format is 00:00:00:00:00:01/-1
*
* @param id id string
* @return HostId if the id is valid, null otherwise
*/
private HostId isHostId(String id) {
if (id.length() < VLAN_SEPARATOR_OFFSET + 1 ||
id.charAt(FIRST_MAC_ADDRESS_SEPARATOR_OFFSET) != ':' ||
id.charAt(SECOND_MAC_ADDRESS_SEPARATOR_OFFSET) != ':' ||
id.charAt(THIRD_MAC_ADDRESS_SEPARATOR_OFFSET) != ':' ||
id.charAt(VLAN_SEPARATOR_OFFSET) != '/') {
if (!id.matches("..:..:..:..:..:../.*")) {
return null;
}
......