Fix NPE when a POST operation is done for multiple flows and there is no flows array specified
Change-Id: I59caa97d8b2c663f1a7765c1d4c50f3b9ec63cca
diff --git a/web/api/src/main/java/org/onosproject/rest/resources/FlowsWebResource.java b/web/api/src/main/java/org/onosproject/rest/resources/FlowsWebResource.java
index e026662..fa5c3c7 100644
--- a/web/api/src/main/java/org/onosproject/rest/resources/FlowsWebResource.java
+++ b/web/api/src/main/java/org/onosproject/rest/resources/FlowsWebResource.java
@@ -50,6 +50,7 @@
import java.util.List;
import java.util.stream.StreamSupport;
+import static org.onlab.util.Tools.nullIsIllegal;
import static org.onlab.util.Tools.nullIsNotFound;
/**
@@ -65,6 +66,7 @@
private static final String DEVICE_NOT_FOUND = "Device is not found";
private static final String FLOW_NOT_FOUND = "Flow is not found";
private static final String APP_ID_NOT_FOUND = "Application Id is not found";
+ private static final String FLOW_ARRAY_REQUIRED = "Flows array was not specified";
private static final String FLOWS = "flows";
private static final String DEVICE_ID = "deviceId";
private static final String FLOW_ID = "flowId";
@@ -115,13 +117,15 @@
public Response createFlows(@QueryParam("appId") String appId, InputStream stream) {
try {
ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
- ArrayNode flowsArray = (ArrayNode) jsonTree.get(FLOWS);
+ ArrayNode flowsArray = nullIsIllegal((ArrayNode) jsonTree.get(FLOWS),
+ FLOW_ARRAY_REQUIRED);
if (appId != null) {
flowsArray.forEach(flowJson -> ((ObjectNode) flowJson).put("appId", appId));
}
List<FlowRule> rules = codec(FlowRule.class).decode(flowsArray, this);
+
service.applyFlowRules(rules.toArray(new FlowRule[rules.size()]));
rules.forEach(flowRule -> {
ObjectNode flowNode = mapper().createObjectNode();