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;
}
/**
diff --git a/core/net/src/test/java/org/onosproject/net/host/impl/BasicHostOperatorTest.java b/core/net/src/test/java/org/onosproject/net/host/impl/BasicHostOperatorTest.java
index 3d1331a..4f18efc 100644
--- a/core/net/src/test/java/org/onosproject/net/host/impl/BasicHostOperatorTest.java
+++ b/core/net/src/test/java/org/onosproject/net/host/impl/BasicHostOperatorTest.java
@@ -17,6 +17,7 @@
import static org.junit.Assert.assertEquals;
+import com.google.common.collect.Sets;
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.IpAddress;
@@ -59,7 +60,7 @@
@Before
public void setUp() {
BHC.init(ID, "test", JsonNodeFactory.instance.objectNode(), mapper, delegate);
- BHC.name(NAME).latitude(40.96);
+ BHC.setLocations(Sets.newHashSet(LOC)).name(NAME).latitude(40.96);
// if you set lat or long, the other becomes valid as 0.0 (not null)
}