if (NetUtils.sameSubnet(alias.getIp4Address(),nic.getIp4Address(),nic.getNetmask())){
ipInVmsubnet = true;
break;
}
}
PublicIp routerPublicIP = null;
String routerAliasIp =null;
DataCenter dc = _dcDao.findById(router.getDataCenterId());
if (ipInVmsubnet == false) {
try {
if (network.getTrafficType() == TrafficType.Guest && network.getGuestType() == GuestType.Shared) {
Pod pod = _podDao.findById(vm.getPodIdToDeployIn());
Account caller = CallContext.current().getCallingAccount();
List<VlanVO> vlanList = _vlanDao.listVlansByNetworkIdAndGateway(network.getId(), nic.getGateway());
List<Long> vlanDbIdList = new ArrayList<Long>();
for (VlanVO vlan : vlanList) {
vlanDbIdList.add(vlan.getId());
}
if (dc.getNetworkType() == NetworkType.Basic) {
routerPublicIP = _ipAddrMgr.assignPublicIpAddressFromVlans(router.getDataCenterId(),
vm.getPodIdToDeployIn(),
caller,
Vlan.VlanType.DirectAttached,
vlanDbIdList,
nic.getNetworkId(),
null,
false);
}
else {
routerPublicIP = _ipAddrMgr.assignPublicIpAddressFromVlans(router.getDataCenterId(),
null,
caller,
Vlan.VlanType.DirectAttached,
vlanDbIdList,
nic.getNetworkId(),
null,
false);
}
routerAliasIp = routerPublicIP.getAddress().addr();
}
}
catch (InsufficientAddressCapacityException e){
s_logger.info(e.getMessage());
s_logger.info("unable to configure dhcp for this VM.");
return false;
}
//this means we did not create a ip alis on the router.
NicIpAliasVO alias = new NicIpAliasVO(domr_guest_nic.getId(), routerAliasIp, router.getId(), CallContext.current().getCallingAccountId(), network.getDomainId(), nic.getNetworkId(),nic.getGateway(), nic.getNetmask());
alias.setAliasCount((routerPublicIP.getIpMacAddress()));
_nicIpAliasDao.persist(alias);
List<IpAliasTO> ipaliasTo = new ArrayList<IpAliasTO>();
ipaliasTo.add(new IpAliasTO(routerAliasIp, alias.getNetmask(), alias.getAliasCount().toString()));
Commands cmds = new Commands(Command.OnError.Stop);
createIpAlias(router, ipaliasTo, alias.getNetworkId(), cmds);
//also add the required configuration to the dnsmasq for supporting dhcp and dns on the new ip.
configDnsMasq(router, network, cmds);
boolean result = sendCommandsToRouter(router, cmds);
if (result == false) {
final NicIpAliasVO ipAliasVO = _nicIpAliasDao.findByInstanceIdAndNetworkId(network.getId(), router.getId());
final PublicIp routerPublicIPFinal = routerPublicIP;
Transaction.execute(new TransactionCallbackNoReturn() {
@Override
public void doInTransactionWithoutResult(TransactionStatus status) {
_nicIpAliasDao.expunge(ipAliasVO.getId());
_ipAddressDao.unassignIpAddress(routerPublicIPFinal.getId());
}
});
throw new CloudRuntimeException("failed to configure ip alias on the router as a part of dhcp config");
}
}