ONOS-6786 Fix NPE when a host netcfg without location being posted
Change-Id: Iae73ed44232b916dd97b60da8443a540d4bf6a96
diff --git a/core/api/src/main/java/org/onosproject/net/config/basics/BasicHostConfig.java b/core/api/src/main/java/org/onosproject/net/config/basics/BasicHostConfig.java
index 1a8db33..3d98d08 100644
--- a/core/api/src/main/java/org/onosproject/net/config/basics/BasicHostConfig.java
+++ b/core/api/src/main/java/org/onosproject/net/config/basics/BasicHostConfig.java
@@ -35,7 +35,8 @@
@Override
public boolean isValid() {
- // Location and IP addresses can be absent, but if present must be valid.
+ // locations is mandatory and must have at least one
+ // ipAddresses can be absent, but if present must be valid
this.locations();
this.ipAddresses();
return hasOnlyFields(ALLOWED, NAME, LOC_TYPE, LATITUDE, LONGITUDE,
@@ -56,7 +57,7 @@
/**
* Returns the location of the host.
*
- * @return location of the host or null if not set
+ * @return location of the host
* @throws IllegalArgumentException if not specified with correct format
*/
public Set<HostLocation> locations() {
@@ -67,9 +68,11 @@
ConnectPoint cp = ConnectPoint.deviceConnectPoint((n.asText()));
locations.add(new HostLocation(cp, 0));
});
- return locations;
}
- return null;
+ if (locations.isEmpty()) {
+ throw new IllegalArgumentException("Host should have at least one location");
+ }
+ return locations;
}
/**