[CORD-2320] DHCPv4 thows an exception when no indirect DHCPv4 server is configured
Change-Id: I516a75f1108ca1245267a45b805153e5eba003d8
diff --git a/apps/dhcprelay/src/main/java/org/onosproject/dhcprelay/Dhcp4HandlerImpl.java b/apps/dhcprelay/src/main/java/org/onosproject/dhcprelay/Dhcp4HandlerImpl.java
index 6c5390c..2c8cf92 100644
--- a/apps/dhcprelay/src/main/java/org/onosproject/dhcprelay/Dhcp4HandlerImpl.java
+++ b/apps/dhcprelay/src/main/java/org/onosproject/dhcprelay/Dhcp4HandlerImpl.java
@@ -442,16 +442,21 @@
}
private Interface getServerInterface(List<DhcpServerInfo> serverInfos) {
- DhcpServerInfo serverInfo = serverInfos.get(0);
- ConnectPoint dhcpServerConnectPoint = serverInfo.getDhcpServerConnectPoint().orElse(null);
- VlanId dhcpConnectVlan = serverInfo.getDhcpConnectVlan().orElse(null);
- if (dhcpServerConnectPoint == null || dhcpConnectVlan == null) {
- return null;
- }
- return interfaceService.getInterfacesByPort(dhcpServerConnectPoint)
- .stream()
- .filter(iface -> interfaceContainsVlan(iface, dhcpConnectVlan))
+ return serverInfos.stream()
.findFirst()
+ .map(serverInfo -> {
+ ConnectPoint dhcpServerConnectPoint =
+ serverInfo.getDhcpServerConnectPoint().orElse(null);
+ VlanId dhcpConnectVlan = serverInfo.getDhcpConnectVlan().orElse(null);
+ if (dhcpServerConnectPoint == null || dhcpConnectVlan == null) {
+ return null;
+ }
+ return interfaceService.getInterfacesByPort(dhcpServerConnectPoint)
+ .stream()
+ .filter(iface -> interfaceContainsVlan(iface, dhcpConnectVlan))
+ .findFirst()
+ .orElse(null);
+ })
.orElse(null);
}
@@ -683,7 +688,16 @@
return null;
}
boolean isDirectlyConnected = directlyConnected(dhcpPacket);
- Interface serverInterface = isDirectlyConnected ? getDefaultServerInterface() : getIndirectServerInterface();
+ Interface serverInterface;
+ if (isDirectlyConnected) {
+ serverInterface = getDefaultServerInterface();
+ } else {
+ serverInterface = getIndirectServerInterface();
+ if (serverInterface == null) {
+ // Indirect server interface not found, use default server interface
+ serverInterface = getDefaultServerInterface();
+ }
+ }
if (serverInterface == null) {
log.warn("Can't get {} server interface, ignore", isDirectlyConnected ? "direct" : "indirect");
return null;
@@ -835,7 +849,16 @@
return null;
}
boolean isDirectlyConnected = directlyConnected(dhcpPacket);
- Interface serverInterface = isDirectlyConnected ? getDefaultServerInterface() : getIndirectServerInterface();
+ Interface serverInterface;
+ if (isDirectlyConnected) {
+ serverInterface = getDefaultServerInterface();
+ } else {
+ serverInterface = getIndirectServerInterface();
+ if (serverInterface == null) {
+ // Indirect server interface not found, use default server interface
+ serverInterface = getDefaultServerInterface();
+ }
+ }
if (serverInterface == null) {
log.warn("Can't get {} server interface, ignore", isDirectlyConnected ? "direct" : "indirect");
return null;
diff --git a/apps/dhcprelay/src/main/java/org/onosproject/dhcprelay/Dhcp6HandlerImpl.java b/apps/dhcprelay/src/main/java/org/onosproject/dhcprelay/Dhcp6HandlerImpl.java
index b4571e6..dcfc901 100644
--- a/apps/dhcprelay/src/main/java/org/onosproject/dhcprelay/Dhcp6HandlerImpl.java
+++ b/apps/dhcprelay/src/main/java/org/onosproject/dhcprelay/Dhcp6HandlerImpl.java
@@ -804,7 +804,16 @@
UDP clientUdp = (UDP) clientIpv6.getPayload();
DHCP6 clientDhcp6 = (DHCP6) clientUdp.getPayload();
boolean directConnFlag = directlyConnected(clientDhcp6);
- Interface serverInterface = directConnFlag ? getServerInterface() : getIndirectServerInterface();
+ Interface serverInterface;
+ if (directConnFlag) {
+ serverInterface = getServerInterface();
+ } else {
+ serverInterface = getIndirectServerInterface();
+ if (serverInterface == null) {
+ // Indirect server interface not found, use default server interface
+ serverInterface = getServerInterface();
+ }
+ }
if (serverInterface == null) {
log.warn("Can't get {} server interface, ignore", directConnFlag ? "direct" : "indirect");
return null;