Bridge netBridge = getOrCreateNetworkBridge(network);
// On bridge, get DHCP subnet (ensure it exists)
ResourceCollection res = netBridge.getDhcpSubnets();
DhcpSubnet sub = null;
if (!res.isEmpty()) {
sub = (DhcpSubnet)res.get(0);
} else {
Pair<String, Integer> cidrInfo = NetUtils.getCidr(network.getCidr());
sub = netBridge.addDhcpSubnet();
sub.subnetLength(cidrInfo.second());
sub.subnetPrefix(cidrInfo.first());
sub.defaultGateway(network.getGateway());
List<String> dcs = new ArrayList<String>();
dcs.add(dest.getDataCenter().getDns1());
sub.dnsServerAddrs(dcs);
sub.create();
}
// On DHCP subnet, add host using host details
if (sub == null) {
s_logger.error("Failed to create DHCP subnet on Midonet bridge");
return false;
} else {
// Check if the host already exists - we may just be restarting an existing VM
boolean isNewDhcpHost = true;
for (DhcpHost dhcpHost : sub.getDhcpHosts()) {
if (dhcpHost.getIpAddr().equals(nic.getIp4Address())) {
isNewDhcpHost = false;
break;
}
}
if (isNewDhcpHost) {
DhcpHost host = sub.addDhcpHost();
host.ipAddr(nic.getIp4Address());
host.macAddr(nic.getMacAddress());
// This only sets the cloudstack internal name
host.name(vm.getHostName());