[ONOS-8150][VOL-4414] Fix for NPE in rest API after app gets uninstalled
Change-Id: I1cf0b330af43be8ea2211c5831ef715c1eafdea9
(cherry picked from commit 365751943b0c5316e79fbc1201a8d7af659e5008)
diff --git a/utils/misc/src/main/java/org/onlab/util/Tools.java b/utils/misc/src/main/java/org/onlab/util/Tools.java
index 596e34b..2dd43a0 100644
--- a/utils/misc/src/main/java/org/onlab/util/Tools.java
+++ b/utils/misc/src/main/java/org/onlab/util/Tools.java
@@ -188,6 +188,7 @@
*/
public static <T> T nullIsNotFound(T item, String message) {
if (item == null) {
+ log.error(message);
throw new ItemNotFoundException(message);
}
return item;
@@ -205,6 +206,7 @@
*/
public static <T> Set<T> emptyIsNotFound(Set<T> item, String message) {
if (item == null || item.isEmpty()) {
+ log.error(message);
throw new ItemNotFoundException(message);
}
return item;
@@ -222,6 +224,7 @@
*/
public static <T> T nullIsIllegal(T item, String message) {
if (item == null) {
+ log.error(message);
throw new IllegalArgumentException(message);
}
return item;
diff --git a/web/api/src/main/java/org/onosproject/rest/resources/ApplicationsWebResource.java b/web/api/src/main/java/org/onosproject/rest/resources/ApplicationsWebResource.java
index b2ee977..3787113 100644
--- a/web/api/src/main/java/org/onosproject/rest/resources/ApplicationsWebResource.java
+++ b/web/api/src/main/java/org/onosproject/rest/resources/ApplicationsWebResource.java
@@ -39,7 +39,9 @@
import java.io.InputStream;
import java.net.URL;
import java.util.Set;
+import org.slf4j.Logger;
+import static org.slf4j.LoggerFactory.getLogger;
import static org.onlab.util.Tools.nullIsNotFound;
import static org.onlab.util.Tools.readTreeFromStream;
@@ -49,6 +51,9 @@
@Path("applications")
public class ApplicationsWebResource extends AbstractWebResource {
+ private static final Logger log = getLogger(ApplicationsWebResource.class);
+
+
private static final String APP_ID_NOT_FOUND = "Application ID is not found";
private static final String APP_NOT_FOUND = "Application is not found";
private static final String APP_READY = "ready";
@@ -83,7 +88,7 @@
@Path("{name}")
public Response getApp(@PathParam("name") String name) {
ApplicationAdminService service = get(ApplicationAdminService.class);
- ApplicationId appId = nullIsNotFound(service.getId(name), APP_NOT_FOUND);
+ ApplicationId appId = nullIsNotFound(service.getId(name), APP_NOT_FOUND + ":" + name);
return response(service, appId);
}
@@ -97,10 +102,14 @@
@Path("{name}/health")
public Response health(@PathParam("name") String name) {
ApplicationAdminService service = get(ApplicationAdminService.class);
- ApplicationId appId = nullIsNotFound(service.getId(name), APP_NOT_FOUND);
+ ApplicationId appId = service.getId(name);
+ nullIsNotFound(appId, APP_ID_NOT_FOUND + ": " + name);
+
+ Application app = service.getApplication(appId);
+ nullIsNotFound(app, APP_NOT_FOUND + ": " + appId);
ComponentsMonitorService componentsMonitorService = get(ComponentsMonitorService.class);
- boolean ready = componentsMonitorService.isFullyStarted(service.getApplication(appId).features());
+ boolean ready = componentsMonitorService.isFullyStarted(app.features());
return Response.ok(mapper().createObjectNode().put("message", ready ? APP_READY : APP_PENDING)).build();
}
@@ -193,7 +202,7 @@
@Path("{name}/active")
public Response activateApp(@PathParam("name") String name) {
ApplicationAdminService service = get(ApplicationAdminService.class);
- ApplicationId appId = nullIsNotFound(service.getId(name), APP_NOT_FOUND);
+ ApplicationId appId = nullIsNotFound(service.getId(name), APP_NOT_FOUND + ": " + name);
service.activate(appId);
return response(service, appId);
}
@@ -245,7 +254,7 @@
@Path("{name}/bits")
public Response getAppBits(@PathParam("name") String name) {
ApplicationAdminService service = get(ApplicationAdminService.class);
- ApplicationId appId = nullIsNotFound(service.getId(name), APP_ID_NOT_FOUND);
+ ApplicationId appId = nullIsNotFound(service.getId(name), APP_ID_NOT_FOUND + ": " + name);
InputStream bits = service.getApplicationArchive(appId);
return ok(bits).build();
}
@@ -290,12 +299,13 @@
}
private Response response(ApplicationAdminService service, ApplicationId appId) {
- Application app = nullIsNotFound(service.getApplication(appId), APP_NOT_FOUND);
+ Application app = nullIsNotFound(service.getApplication(appId),
+ APP_NOT_FOUND + ": " + appId);
return ok(codec(Application.class).encode(app, this)).build();
}
private Response response(ApplicationId appId) {
- ApplicationId checkedAppId = nullIsNotFound(appId, APP_ID_NOT_FOUND);
+ ApplicationId checkedAppId = nullIsNotFound(appId, APP_ID_NOT_FOUND + ": " + appId);
return ok(codec(ApplicationId.class).encode(checkedAppId, this)).build();
}
}