s_logger.warn("Failed to configure dhcp: router not in running state");
throw new ResourceUnavailableException("Unable to assign ip addresses, domR is not in right state " +
router.getState(), DataCenter.class, network.getDataCenterId());
}
//check if this is not the primary subnet.
NicVO domr_guest_nic = _nicDao.findByInstanceIdAndIpAddressAndVmtype(router.getId(), _nicDao.getIpAddress(nic.getNetworkId(), router.getId()), VirtualMachine.Type.DomainRouter);
//check if the router ip address and the vm ip address belong to same subnet.
//if they do not belong to same netwoek check for the alias ips. if not create one.
// This should happen only in case of Basic and Advanced SG enabled networks.
if (!NetUtils.sameSubnet(domr_guest_nic.getIp4Address(), nic.getIp4Address(), nic.getNetmask())){
List<NicIpAliasVO> aliasIps = _nicIpAliasDao.listByNetworkIdAndState(domr_guest_nic.getNetworkId(), NicIpAlias.state.active);
boolean ipInVmsubnet =false;
for (NicIpAliasVO alias : aliasIps) {
//check if any of the alias ips belongs to the Vm's subnet.
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);