Revert "Fix for ONOS-5417:Issue with Creating Flows using POST flows REST API"

This reverts commit dba61119d17fcffa67026d1cf7e450fc7fb2a2ec.

This breaks compatibility with the existing API where users can POST a flow without specifying an app ID.

Change-Id: I50b4be3cb018a2d5a5fe61153d6c491df49122e2
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 e9f88ba..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
@@ -116,14 +116,13 @@
     @Produces(MediaType.APPLICATION_JSON)
     public Response createFlows(@QueryParam("appId") String appId, InputStream stream) {
         try {
-            final ApplicationService appService = get(ApplicationService.class);
-            final ApplicationId idInstant = nullIsNotFound(appService.getId(appId), APP_ID_NOT_FOUND);
             ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
             ArrayNode flowsArray = nullIsIllegal((ArrayNode) jsonTree.get(FLOWS),
                                                  FLOW_ARRAY_REQUIRED);
 
-            flowsArray.forEach(flowJson -> ((ObjectNode) flowJson).put("appId", idInstant.name()));
-
+            if (appId != null) {
+                flowsArray.forEach(flowJson -> ((ObjectNode) flowJson).put("appId", appId));
+            }
 
             List<FlowRule> rules = codec(FlowRule.class).decode(flowsArray, this);
 
@@ -254,8 +253,6 @@
                                @QueryParam("appId") String appId,
                                InputStream stream) {
         try {
-            final ApplicationService appService = get(ApplicationService.class);
-            final ApplicationId idInstant = nullIsNotFound(appService.getId(appId), APP_ID_NOT_FOUND);
             ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
             JsonNode specifiedDeviceId = jsonTree.get("deviceId");
             if (specifiedDeviceId != null &&
@@ -265,7 +262,9 @@
             }
             jsonTree.put("deviceId", deviceId);
 
-            jsonTree.put("appId", idInstant.name());
+            if (appId != null) {
+                jsonTree.put("appId", appId);
+            }
 
             FlowRule rule = codec(FlowRule.class).decode(jsonTree, this);
             service.applyFlowRules(rule);
diff --git a/web/api/src/test/java/org/onosproject/rest/resources/FlowsResourceTest.java b/web/api/src/test/java/org/onosproject/rest/resources/FlowsResourceTest.java
index 74defed..2ecd373 100644
--- a/web/api/src/test/java/org/onosproject/rest/resources/FlowsResourceTest.java
+++ b/web/api/src/test/java/org/onosproject/rest/resources/FlowsResourceTest.java
@@ -33,6 +33,7 @@
 import org.onosproject.app.ApplicationService;
 import org.onosproject.codec.CodecService;
 import org.onosproject.codec.impl.CodecManager;
+import org.onosproject.codec.impl.FlowRuleCodec;
 import org.onosproject.core.CoreService;
 import org.onosproject.core.DefaultGroupId;
 import org.onosproject.core.GroupId;
@@ -45,6 +46,7 @@
 import org.onosproject.net.flow.DefaultTrafficTreatment;
 import org.onosproject.net.flow.FlowEntry;
 import org.onosproject.net.flow.FlowId;
+import org.onosproject.net.flow.FlowRule.FlowRemoveReason;
 import org.onosproject.net.flow.FlowRule;
 import org.onosproject.net.flow.FlowRuleExtPayLoad;
 import org.onosproject.net.flow.FlowRuleService;
@@ -402,7 +404,7 @@
                 .andReturn(NetTestTools.APP_ID).anyTimes();
         expect(mockCoreService.getAppId(anyString()))
                 .andReturn(NetTestTools.APP_ID).anyTimes();
-        expect(mockCoreService.registerApplication(anyString()))
+        expect(mockCoreService.registerApplication(FlowRuleCodec.REST_APP_ID))
                 .andReturn(APP_ID).anyTimes();
         replay(mockCoreService);
 
@@ -878,6 +880,8 @@
      */
     @Test
     public void testPostWithoutAppId() {
+        mockFlowService.applyFlowRules(anyObject());
+        expectLastCall();
         replay(mockFlowService);
 
         WebTarget wt = target();
@@ -887,7 +891,9 @@
         Response response = wt.path("flows/of:0000000000000001")
                 .request(MediaType.APPLICATION_JSON_TYPE)
                 .post(Entity.json(jsonStream));
-        assertThat(response.getStatus(), is(HttpURLConnection.HTTP_NOT_FOUND));
+        assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED));
+        String location = response.getLocation().getPath();
+        assertThat(location, Matchers.startsWith("/flows/of:0000000000000001/"));
     }
 
     /**
@@ -899,9 +905,6 @@
         expectLastCall();
         replay(mockFlowService);
 
-        expect(mockApplicationService.getId(anyObject())).andReturn(APP_ID).anyTimes();
-        replay(mockApplicationService);
-
         WebTarget wt = target();
         InputStream jsonStream = FlowsResourceTest.class
                 .getResourceAsStream("post-flow.json");