diff --git a/server/src/com/cloud/ha/InvestigatorImpl.java b/server/src/com/cloud/ha/InvestigatorImpl.java index eda08a88cf1..d884b61512d 100644 --- a/server/src/com/cloud/ha/InvestigatorImpl.java +++ b/server/src/com/cloud/ha/InvestigatorImpl.java @@ -31,13 +31,16 @@ import com.cloud.agent.api.Answer; import com.cloud.agent.api.PingTestCommand; import com.cloud.exception.AgentUnavailableException; import com.cloud.exception.OperationTimedoutException; -import com.cloud.host.Host; +import com.cloud.host.Host.Type; import com.cloud.host.HostVO; import com.cloud.host.Status; import com.cloud.host.dao.HostDao; -import com.cloud.utils.component.ComponentLocator; -import com.cloud.utils.db.SearchCriteria; -import com.cloud.vm.DomainRouterVO; +import com.cloud.network.NetworkManager; +import com.cloud.network.Networks.TrafficType; +import com.cloud.network.router.VirtualNetworkApplianceManager; +import com.cloud.network.router.VirtualRouter; +import com.cloud.utils.component.Inject; +import com.cloud.vm.Nic; import com.cloud.vm.UserVmVO; import com.cloud.vm.VMInstanceVO; import com.cloud.vm.VirtualMachine; @@ -49,10 +52,12 @@ public class InvestigatorImpl implements Investigator { private static final Logger s_logger = Logger.getLogger(InvestigatorImpl.class); private String _name = null; - private HostDao _hostDao = null; - private DomainRouterDao _routerDao = null;; - private UserVmDao _userVmDao = null; - private AgentManager _agentMgr = null; + @Inject private HostDao _hostDao = null; + @Inject private DomainRouterDao _routerDao = null;; + @Inject private UserVmDao _userVmDao = null; + @Inject private AgentManager _agentMgr = null; + @Inject private NetworkManager _networkMgr = null; + @Inject private VirtualNetworkApplianceManager _vnaMgr = null; @Override public Boolean isVmAlive(VMInstanceVO vm, HostVO host) { @@ -61,22 +66,32 @@ public class InvestigatorImpl implements Investigator { } if (vm.getType() == VirtualMachine.Type.User) { // to verify that the VM is alive, we ask the domR (router) to ping the VM (private IP) - UserVmVO userVm = _userVmDao.findById(vm.getId()); - Long routerId = null; // FIXME: This doesn't work. Need to grab the domain router from the network. - if (routerId == null) { - /*TODO: checking vm status for external dhcp mode*/ - s_logger.debug("It's external dhcp mode, how to checking the vm is alive?"); - return true; - } else { - return testUserVM(vm, routerId); - } + UserVmVO userVm = _userVmDao.findById(vm.getId()); + + Nic nic = _networkMgr.getNicForTraffic(userVm.getId(), TrafficType.Guest); + if (nic == null) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Unable to find a guest nic for " + vm); + } + return null; + } + + VirtualRouter router = _vnaMgr.getRouterForNetwork(nic.getNetworkId()); + if (router == null) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Unable to find a router in network " + nic.getNetworkId() + " to ping " + vm); + } + return null; + } + + return testUserVM(vm, nic, router); } else if ((vm.getType() == VirtualMachine.Type.DomainRouter) || (vm.getType() == VirtualMachine.Type.ConsoleProxy)) { // get the data center IP address, find a host on the pod, use that host to ping the data center IP address HostVO vmHost = _hostDao.findById(vm.getHostId()); - List otherHosts = findHostByPod(vm.getPodId(), vm.getHostId()); - for (HostVO otherHost : otherHosts) { + List otherHosts = findHostByPod(vm.getPodId(), vm.getHostId()); + for (Long otherHost : otherHosts) { - Status vmState = testIpAddress(otherHost.getId(), vm.getPrivateIpAddress()); + Status vmState = testIpAddress(otherHost, vm.getPrivateIpAddress()); if (vmState == null) { // can't get information from that host, try the next one continue; @@ -89,7 +104,7 @@ public class InvestigatorImpl implements Investigator { } else if (vmState == Status.Down) { // We can't ping the VM directly...if we can ping the host, then report the VM down. // If we can't ping the host, then we don't have enough information. - Status vmHostState = testIpAddress(otherHost.getId(), vmHost.getPrivateIpAddress()); + Status vmHostState = testIpAddress(otherHost, vmHost.getPrivateIpAddress()); if ((vmHostState != null) && (vmHostState == Status.Up)) { if (s_logger.isDebugEnabled()) { s_logger.debug("successfully pinged vm's host IP (" + vmHost.getPrivateIpAddress() + "), but could not ping VM, returning that the VM is down"); @@ -107,29 +122,28 @@ public class InvestigatorImpl implements Investigator { @Override public Status isAgentAlive(HostVO agent) { - Long hostId = agent.getId(); if (s_logger.isDebugEnabled()) { - s_logger.debug("checking if agent (" + hostId + ") is alive"); + s_logger.debug("checking if agent (" + agent.getId() + ") is alive"); } if (agent.getPodId() == null) { return null; } - List otherHosts = findHostByPod(agent.getPodId(), agent.getId()); + List otherHosts = findHostByPod(agent.getPodId(), agent.getId()); - for (HostVO otherHost : otherHosts) { + for (Long hostId : otherHosts) { if (s_logger.isDebugEnabled()) { - s_logger.debug("sending ping from (" + otherHost.getId() + ") to agent's host ip address (" + agent.getPrivateIpAddress() + ")"); + s_logger.debug("sending ping from (" + hostId + ") to agent's host ip address (" + agent.getPrivateIpAddress() + ")"); } - Status hostState = testIpAddress(otherHost.getId(), agent.getPrivateIpAddress()); + Status hostState = testIpAddress(hostId, agent.getPrivateIpAddress()); if (hostState == null) { continue; } if (hostState == Status.Up) { if (s_logger.isDebugEnabled()) { - s_logger.debug("ping from (" + otherHost.getId() + ") to agent's host ip address (" + agent.getPrivateIpAddress() + ") successful, returning that agent is disconnected"); + s_logger.debug("ping from (" + hostId + ") to agent's host ip address (" + agent.getPrivateIpAddress() + ") successful, returning that agent is disconnected"); } return Status.Disconnected; // the computing host ip is ping-able, but the computing agent is down, report that the agent is disconnected } else if (hostState == Status.Down) { @@ -150,11 +164,6 @@ public class InvestigatorImpl implements Investigator { @Override public boolean configure(String name, Map params) throws ConfigurationException { _name = name; - ComponentLocator locator = ComponentLocator.getCurrentLocator(); - _hostDao = locator.getDao(HostDao.class); - _routerDao = locator.getDao(DomainRouterDao.class); - _userVmDao = locator.getDao(UserVmDao.class); - _agentMgr = locator.getManager(AgentManager.class); return true; } @@ -172,29 +181,26 @@ public class InvestigatorImpl implements Investigator { @Override public boolean stop() { return true; - } + } + // Host.status is up and Host.type is routing - private List findHostByPod(long podId, Long excludeHostId) { - SearchCriteria sc = _hostDao.createSearchCriteria(); - sc.addAnd("podId", SearchCriteria.Op.EQ, podId); - sc.addAnd("status", SearchCriteria.Op.EQ, Status.Up); - sc.addAnd("type", SearchCriteria.Op.EQ, Host.Type.Routing); - if (excludeHostId != null) { - sc.addAnd("id", SearchCriteria.Op.NEQ, excludeHostId); - } - return _hostDao.search(sc, null); + private List findHostByPod(long podId, Long excludeHostId) { + List hostIds = _hostDao.listBy(null, podId, null, Type.Routing, Status.Up); + if (excludeHostId != null){ + hostIds.remove(excludeHostId); + } + + return hostIds; } - private Boolean testUserVM(VMInstanceVO vm, Long routerId) { - DomainRouterVO router = _routerDao.findById(routerId); - String privateIp = vm.getPrivateIpAddress(); + private Boolean testUserVM(VMInstanceVO vm, Nic nic, VirtualRouter router) { + String privateIp = nic.getIp4Address(); String routerPrivateIp = router.getPrivateIpAddress(); - List otherHosts = findHostByPod(router.getPodId(), null); - for (HostVO otherHost : otherHosts) { - + List otherHosts = findHostByPod(router.getPodId(), null); + for (Long hostId : otherHosts) { try { - Answer pingTestAnswer = _agentMgr.send(otherHost.getId(), new PingTestCommand(routerPrivateIp, privateIp), 30 * 1000); + Answer pingTestAnswer = _agentMgr.send(hostId, new PingTestCommand(routerPrivateIp, privateIp), 30 * 1000); if (pingTestAnswer.getResult()) { if (s_logger.isDebugEnabled()) { s_logger.debug("user vm " + vm.getName() + " has been successfully pinged, returning that it is alive"); @@ -214,7 +220,7 @@ public class InvestigatorImpl implements Investigator { } } if (s_logger.isDebugEnabled()) { - s_logger.debug("user vm " + vm.getName() + " could not be pinged, returning that it is unknown"); + s_logger.debug(vm + " could not be pinged, returning that it is unknown"); } return null; diff --git a/server/src/com/cloud/host/dao/HostDao.java b/server/src/com/cloud/host/dao/HostDao.java index fff04e21067..0b6c6f4c794 100644 --- a/server/src/com/cloud/host/dao/HostDao.java +++ b/server/src/com/cloud/host/dao/HostDao.java @@ -21,9 +21,9 @@ import java.util.Date; import java.util.List; import com.cloud.host.Host; +import com.cloud.host.Host.Type; import com.cloud.host.HostVO; import com.cloud.host.Status; -import com.cloud.host.Host.Type; import com.cloud.host.Status.Event; import com.cloud.hypervisor.Hypervisor.HypervisorType; import com.cloud.info.RunningHostCountInfo; @@ -140,5 +140,16 @@ public interface HostDao extends GenericDao { HostVO findConsoleProxyHost(String name, Type type); - List getAvailHypervisorInZone(long hostId, long zoneId); + List getAvailHypervisorInZone(long hostId, long zoneId); + + /** + * Returns a list of host ids given the conditions. + * @param dataCenterId if specified, then must be in this data center. + * @param podId if specified, then must be in this pod. + * @param clusterId if specified, then must be in this cluster. + * @param hostType TODO + * @param statuses the host needs to be in. + * @return ids of the host meeting the search parameters. + */ + List listBy(Long dataCenterId, Long podId, Long clusterId, Type hostType, Status... statuses); } diff --git a/server/src/com/cloud/host/dao/HostDaoImpl.java b/server/src/com/cloud/host/dao/HostDaoImpl.java index 4e01bcbc98b..bfdb5682a91 100644 --- a/server/src/com/cloud/host/dao/HostDaoImpl.java +++ b/server/src/com/cloud/host/dao/HostDaoImpl.java @@ -27,15 +27,14 @@ import java.util.Map; import java.util.TimeZone; import javax.ejb.Local; -import javax.naming.ConfigurationException; import javax.persistence.TableGenerator; import org.apache.log4j.Logger; import com.cloud.host.Host; +import com.cloud.host.Host.Type; import com.cloud.host.HostVO; import com.cloud.host.Status; -import com.cloud.host.Host.Type; import com.cloud.host.Status.Event; import com.cloud.hypervisor.Hypervisor.HypervisorType; import com.cloud.info.RunningHostCountInfo; @@ -48,9 +47,9 @@ import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.GenericSearchBuilder; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; +import com.cloud.utils.db.SearchCriteria.Op; import com.cloud.utils.db.Transaction; import com.cloud.utils.db.UpdateBuilder; -import com.cloud.utils.db.SearchCriteria.Func; import com.cloud.utils.exception.CloudRuntimeException; @Local(value = { HostDao.class }) @DB(txn=false) @@ -83,6 +82,8 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao protected final SearchBuilder ClusterSearch; protected final SearchBuilder ConsoleProxyHostSearch; protected final SearchBuilder AvailHypevisorInZone; + + protected final GenericSearchBuilder HostsInStatusSearch; protected final Attribute _statusAttr; protected final Attribute _msIdAttr; @@ -215,6 +216,15 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao AvailHypevisorInZone.and("type", AvailHypevisorInZone.entity().getType(), SearchCriteria.Op.EQ); AvailHypevisorInZone.groupBy(AvailHypevisorInZone.entity().getHypervisorType()); AvailHypevisorInZone.done(); + + HostsInStatusSearch = createSearchBuilder(Long.class); + HostsInStatusSearch.selectField(HostsInStatusSearch.entity().getId()); + HostsInStatusSearch.and("dc", HostsInStatusSearch.entity().getDataCenterId(), Op.EQ); + HostsInStatusSearch.and("pod", HostsInStatusSearch.entity().getPodId(), Op.EQ); + HostsInStatusSearch.and("cluster", HostsInStatusSearch.entity().getClusterId(), Op.EQ); + HostsInStatusSearch.and("type", HostsInStatusSearch.entity().getType(), Op.EQ); + HostsInStatusSearch.and("statuses", HostsInStatusSearch.entity().getStatus(), Op.IN); + HostsInStatusSearch.done(); _statusAttr = _allAttributes.get("status"); _msIdAttr = _allAttributes.get("managementServerId"); @@ -458,6 +468,7 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao return listBy(sc); } + @Override public List findHostsLike(String hostName) { SearchCriteria sc = NameLikeSearch.create(); sc.setParameters("name", "%" + hostName + "%"); @@ -485,12 +496,14 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao sc.setParameters("type", type); ListhostList = listBy(sc); - if(hostList==null || hostList.size() == 0) - return null; - else - return hostList.get(0); + if(hostList==null || hostList.size() == 0) { + return null; + } else { + return hostList.get(0); + } } + @Override public List listByHostPod(long podId) { SearchCriteria sc = PodSearch.create("pod", podId); return listBy(sc); @@ -527,15 +540,6 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao _detailsDao.persist(host.getId(), details); } - @Override - public boolean configure(String name, Map params) throws ConfigurationException { - if (!super.configure(name, params)) { - return false; - } - - return true; - } - @Override @DB public HostVO persist(HostVO host) { final String InsertSequenceSql = "INSERT INTO op_host(id) VALUES(?)"; @@ -639,6 +643,30 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao hypers.add(host.getHypervisorType()); } return hypers; + } + + @Override + public List listBy(Long dataCenterId, Long podId, Long clusterId, Type hostType, Status... statuses) { + SearchCriteria sc = HostsInStatusSearch.create(); + if (dataCenterId != null) { + sc.setParameters("dc", dataCenterId); + } + + if (podId != null) { + sc.setParameters("pod", podId); + } + + if (clusterId != null) { + sc.setParameters("cluster", clusterId); + } + + if (hostType != null) { + sc.setParameters("type", hostType); + } + + sc.setParameters("statuses", (Object[])statuses); + + return customSearch(sc, null); } } diff --git a/server/src/com/cloud/network/element/DhcpElement.java b/server/src/com/cloud/network/element/DhcpElement.java index 3546e6aed58..2ce6d8730c2 100644 --- a/server/src/com/cloud/network/element/DhcpElement.java +++ b/server/src/com/cloud/network/element/DhcpElement.java @@ -126,7 +126,7 @@ public class DhcpElement extends AdapterBase implements NetworkElement, Password @Override public boolean shutdown(Network network, ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException { - DomainRouterVO router = _routerDao.findByNetworkConfiguration(network.getId()); + DomainRouterVO router = _routerDao.findByNetwork(network.getId()); if (router == null) { return true; } @@ -135,7 +135,7 @@ public class DhcpElement extends AdapterBase implements NetworkElement, Password @Override public boolean destroy(Network config) throws ConcurrentOperationException, ResourceUnavailableException{ - DomainRouterVO router = _routerDao.findByNetworkConfiguration(config.getId()); + DomainRouterVO router = _routerDao.findByNetwork(config.getId()); if (router == null) { return true; } @@ -178,7 +178,7 @@ public class DhcpElement extends AdapterBase implements NetworkElement, Password DataCenter dc = _configMgr.getZone(network.getDataCenterId()); NetworkOffering offering = _configMgr.getNetworkOffering(network.getNetworkOfferingId()); DeployDestination dest = new DeployDestination(dc, null, null, null); - DomainRouterVO router = _routerDao.findByNetworkConfiguration(network.getId()); + DomainRouterVO router = _routerDao.findByNetwork(network.getId()); if (router == null) { s_logger.trace("Can't find dhcp element in network " + network.getId()); return true; diff --git a/server/src/com/cloud/network/element/VirtualRouterElement.java b/server/src/com/cloud/network/element/VirtualRouterElement.java index 93987b869e2..f58e48fd40c 100644 --- a/server/src/com/cloud/network/element/VirtualRouterElement.java +++ b/server/src/com/cloud/network/element/VirtualRouterElement.java @@ -133,7 +133,7 @@ public class VirtualRouterElement extends DhcpElement implements NetworkElement, public boolean restart(Network network, ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException{ DataCenter dc = _configMgr.getZone(network.getDataCenterId()); DeployDestination dest = new DeployDestination(dc, null, null, null); - DomainRouterVO router = _routerDao.findByNetworkConfiguration(network.getId()); + DomainRouterVO router = _routerDao.findByNetwork(network.getId()); if (router == null) { s_logger.trace("Can't find virtual router element in network " + network.getId()); return true; @@ -165,7 +165,7 @@ public class VirtualRouterElement extends DhcpElement implements NetworkElement, if (canHandle(config.getGuestType(),dc)) { long networkId = config.getId(); - DomainRouterVO router = _routerDao.findByNetworkConfiguration(networkId); + DomainRouterVO router = _routerDao.findByNetwork(networkId); if (router == null) { s_logger.warn("Unable to apply firewall rules, virtual router doesn't exist in the network " + config.getId()); throw new CloudRuntimeException("Unable to apply firewall rules"); diff --git a/server/src/com/cloud/network/router/VirtualNetworkApplianceManager.java b/server/src/com/cloud/network/router/VirtualNetworkApplianceManager.java index 682191262a9..cd628667df7 100644 --- a/server/src/com/cloud/network/router/VirtualNetworkApplianceManager.java +++ b/server/src/com/cloud/network/router/VirtualNetworkApplianceManager.java @@ -21,13 +21,10 @@ import java.util.List; import java.util.Map; import com.cloud.agent.api.to.PortForwardingRuleTO; -import com.cloud.api.commands.UpgradeRouterCmd; import com.cloud.deploy.DeployDestination; import com.cloud.exception.AgentUnavailableException; import com.cloud.exception.ConcurrentOperationException; import com.cloud.exception.InsufficientCapacityException; -import com.cloud.exception.InvalidParameterValueException; -import com.cloud.exception.PermissionDeniedException; import com.cloud.exception.ResourceUnavailableException; import com.cloud.network.Network; import com.cloud.network.PublicIpAddress; @@ -38,7 +35,6 @@ import com.cloud.network.lb.LoadBalancingRule; import com.cloud.user.Account; import com.cloud.uservm.UserVm; import com.cloud.utils.component.Manager; -import com.cloud.vm.DomainRouterVO; import com.cloud.vm.NicProfile; import com.cloud.vm.ReservationContext; import com.cloud.vm.VirtualMachineProfile; @@ -69,11 +65,7 @@ public interface VirtualNetworkApplianceManager extends Manager, VirtualNetworkA boolean getRouterStatistics(long vmId, Map netStats, Map diskStats); - @Override - VirtualRouter upgradeRouter(UpgradeRouterCmd cmd) throws InvalidParameterValueException, PermissionDeniedException; - - DomainRouterVO getRouter(long accountId, long zoneId); - DomainRouterVO getRouter(String publicIpAddress); + VirtualRouter getRouter(long accountId, long zoneId); VirtualRouter deployVirtualRouter(Network guestNetwork, DeployDestination dest, Account owner, Map params) throws InsufficientCapacityException, ResourceUnavailableException, ConcurrentOperationException; @@ -92,4 +84,6 @@ public interface VirtualNetworkApplianceManager extends Manager, VirtualNetworkA String[] applyVpnUsers(Network network, List users) throws ResourceUnavailableException; + VirtualRouter getRouterForNetwork(long networkId); + } diff --git a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java index 4056cd8147c..4d43627b681 100644 --- a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java +++ b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java @@ -291,11 +291,6 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian return _routerDao.findBy(accountId, dataCenterId); } - @Override - public DomainRouterVO getRouter(String publicIpAddress) { - return _routerDao.findByPublicIpAddress(publicIpAddress); - } - @Override public boolean sendSshKeysToHost(Long hostId, String pubKey, String prvKey) { ModifySshKeysCommand cmd = new ModifySshKeysCommand(pubKey, prvKey); @@ -398,7 +393,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian @Override public boolean savePasswordToRouter(Network network, NicProfile nic, VirtualMachineProfile profile) throws ResourceUnavailableException{ - DomainRouterVO router = _routerDao.findByNetworkConfiguration(network.getId()); + DomainRouterVO router = _routerDao.findByNetwork(network.getId()); if (router == null) { s_logger.warn("Unable save password, router doesn't exist in network " + network.getId()); throw new CloudRuntimeException("Unable to save password to router"); @@ -753,7 +748,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian DataCenterDeployment plan = new DataCenterDeployment(dcId); - DomainRouterVO router = _routerDao.findByNetworkConfiguration(guestNetwork.getId()); + DomainRouterVO router = _routerDao.findByNetwork(guestNetwork.getId()); if (router == null) { long id = _routerDao.getNextInSequence(Long.class, "id"); if (s_logger.isDebugEnabled()) { @@ -830,9 +825,9 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian //In Basic zone and Guest network we have to start domR per pod, not per network if (dc.getNetworkType() == NetworkType.Basic && guestNetwork.getTrafficType() == TrafficType.Guest) { - router = _routerDao.findByNetworkConfigurationAndPod(guestNetwork.getId(), podId); + router = _routerDao.findByNetworkAndPod(guestNetwork.getId(), podId); } else { - router = _routerDao.findByNetworkConfiguration(guestNetwork.getId()); + router = _routerDao.findByNetwork(guestNetwork.getId()); } if (router == null) { @@ -1108,7 +1103,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian @Override public boolean startRemoteAccessVpn(Network network, RemoteAccessVpn vpn) throws ResourceUnavailableException { - DomainRouterVO router = _routerDao.findByNetworkConfiguration(network.getId()); + DomainRouterVO router = _routerDao.findByNetwork(network.getId()); if (router == null) { s_logger.warn("Failed to start remote access VPN: no router found for account and zone"); throw new ResourceUnavailableException("Unable to apply lb rules", DataCenter.class, network.getDataCenterId()); @@ -1267,7 +1262,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian @Override public String[] applyVpnUsers(Network network, List users) throws ResourceUnavailableException{ - DomainRouterVO router = _routerDao.findByNetworkConfiguration(network.getId()); + DomainRouterVO router = _routerDao.findByNetwork(network.getId()); if (router == null) { s_logger.warn("Failed to add/remove VPN users: no router found for account and zone"); throw new ResourceUnavailableException("Unable to assign ip addresses, domR doesn't exist for network " + network.getId(), DataCenter.class, network.getDataCenterId()); @@ -1522,12 +1517,12 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian @Override public boolean associateIP(Network network, List ipAddress) throws ResourceUnavailableException { - DomainRouterVO router = _routerDao.findByNetworkConfiguration(network.getId()); + DomainRouterVO router = _routerDao.findByNetwork(network.getId()); if (router == null) { //Return true only when domR entry exists, has Destroyed state and not null Removed field //because it happens just in case when this method is called as a part of account cleanup. //In all other cases return false - router = _routerDao.findByNetworkConfigurationIncludingRemoved(network.getId()); + router = _routerDao.findByNetworkIncludingRemoved(network.getId()); if (router != null && (router.getState() == State.Destroyed || router.getState() == State.Expunging)) { return true; } @@ -1551,7 +1546,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian @Override public boolean applyLBRules(Network network, List rules) throws ResourceUnavailableException { - DomainRouterVO router = _routerDao.findByNetworkConfiguration(network.getId()); + DomainRouterVO router = _routerDao.findByNetwork(network.getId()); if (router == null) { s_logger.warn("Unable to apply lb rules, virtual router doesn't exist in the network " + network.getId()); throw new ResourceUnavailableException("Unable to apply lb rules", DataCenter.class, network.getDataCenterId()); @@ -1565,7 +1560,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian @Override public boolean applyPortForwardingRules(Network network, List rules) throws AgentUnavailableException { - DomainRouterVO router = _routerDao.findByNetworkConfiguration(network.getId()); + DomainRouterVO router = _routerDao.findByNetwork(network.getId()); Commands cmds = new Commands(OnError.Continue); createApplyPortForwardingRulesCommands(rules, router, cmds); @@ -1608,4 +1603,10 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian return routersToStop; } + + @Override + public VirtualRouter getRouterForNetwork(long networkId) { + return _routerDao.findByNetwork(networkId); + + } } diff --git a/server/src/com/cloud/vm/dao/DomainRouterDao.java b/server/src/com/cloud/vm/dao/DomainRouterDao.java index d1cadcc1efc..da904c535f2 100755 --- a/server/src/com/cloud/vm/dao/DomainRouterDao.java +++ b/server/src/com/cloud/vm/dao/DomainRouterDao.java @@ -27,7 +27,7 @@ import com.cloud.vm.DomainRouterVO; * * DomainRouterDao implements */ -public interface DomainRouterDao extends GenericDao{ +public interface DomainRouterDao extends GenericDao { /** * gets the DomainRouterVO by user id and data center * @Param dcId data center Id. @@ -69,20 +69,6 @@ public interface DomainRouterDao extends GenericDao{ */ public List listUpByHostId(Long hostId); - /** - * Finds a domain router based on the ip address it is assigned to. - * @param ipAddress - * @return DomainRouterVO or null if not found. - */ - public DomainRouterVO findByPublicIpAddress(String ipAddress); - - /** - * Gets the next dhcp ip address to be used for vms from this domain router. - * @param id domain router id - * @return next ip address - */ - long getNextDhcpIpAddress(long id); - /** * Find the list of domain routers for a domain * @param id @@ -92,9 +78,9 @@ public interface DomainRouterDao extends GenericDao{ DomainRouterVO findBy(long accountId, long dcId, Role role); - DomainRouterVO findByNetworkConfiguration(long networkConfigurationId); + DomainRouterVO findByNetwork(long networkId); - DomainRouterVO findByNetworkConfigurationIncludingRemoved(long networkConfigurationId); + DomainRouterVO findByNetworkIncludingRemoved(long networkId); - DomainRouterVO findByNetworkConfigurationAndPod(long networkConfigurationId, long podId); + DomainRouterVO findByNetworkAndPod(long networkId, long podId); } diff --git a/server/src/com/cloud/vm/dao/DomainRouterDaoImpl.java b/server/src/com/cloud/vm/dao/DomainRouterDaoImpl.java index bfc0dff65f7..75b1c3bffd4 100755 --- a/server/src/com/cloud/vm/dao/DomainRouterDaoImpl.java +++ b/server/src/com/cloud/vm/dao/DomainRouterDaoImpl.java @@ -17,9 +17,6 @@ */ package com.cloud.vm.dao; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; import java.util.List; import javax.ejb.Local; @@ -27,13 +24,12 @@ import javax.ejb.Local; import org.apache.log4j.Logger; import com.cloud.network.router.VirtualRouter.Role; -import com.cloud.utils.db.Attribute; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; +import com.cloud.utils.db.SearchCriteria.Op; import com.cloud.utils.db.Transaction; import com.cloud.utils.db.UpdateBuilder; -import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.vm.DomainRouterVO; import com.cloud.vm.VirtualMachine.State; @@ -41,92 +37,33 @@ import com.cloud.vm.VirtualMachine.State; public class DomainRouterDaoImpl extends GenericDaoBase implements DomainRouterDao { private static final Logger s_logger = Logger.getLogger(DomainRouterDaoImpl.class); - private static final String GetNextDhcpAddressSql = "UPDATE domain_router set dhcp_ip_address = (@LAST_DHCP:=dhcp_ip_address) + 1 WHERE id = ?"; - private static final String GetLastDhcpSql = "SELECT @LAST_DHCP"; - + protected final SearchBuilder AllFieldsSearch; protected final SearchBuilder IdStatesSearch; - protected final SearchBuilder AccountDcSearch; - protected final SearchBuilder AccountDcRoleSearch; - - protected final SearchBuilder AccountSearch; - protected final SearchBuilder DcSearch; - protected final SearchBuilder IpSearch; - protected final SearchBuilder HostSearch; - protected final SearchBuilder LastHostSearch; protected final SearchBuilder HostUpSearch; - protected final SearchBuilder DomainIdSearch; - protected final SearchBuilder StateChangeSearch; - protected final SearchBuilder NetworkConfigSearch; - protected final Attribute _updateTimeAttr; protected DomainRouterDaoImpl() { - DcSearch = createSearchBuilder(); - DcSearch.and("dc", DcSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ); - DcSearch.done(); + AllFieldsSearch = createSearchBuilder(); + AllFieldsSearch.and("dc", AllFieldsSearch.entity().getDataCenterId(), Op.EQ); + AllFieldsSearch.and("account", AllFieldsSearch.entity().getAccountId(), Op.EQ); + AllFieldsSearch.and("role", AllFieldsSearch.entity().getRole(), Op.EQ); + AllFieldsSearch.and("domainId", AllFieldsSearch.entity().getDomainId(), Op.EQ); + AllFieldsSearch.and("host", AllFieldsSearch.entity().getHostId(), Op.EQ); + AllFieldsSearch.and("lastHost", AllFieldsSearch.entity().getLastHostId(), Op.EQ); + AllFieldsSearch.and("state", AllFieldsSearch.entity().getState(), Op.EQ); + AllFieldsSearch.and("network", AllFieldsSearch.entity().getNetworkId(), Op.EQ); + AllFieldsSearch.and("podId", AllFieldsSearch.entity().getPodId(), Op.EQ); + AllFieldsSearch.done(); IdStatesSearch = createSearchBuilder(); - IdStatesSearch.and("id", IdStatesSearch.entity().getId(), SearchCriteria.Op.EQ); - IdStatesSearch.and("states", IdStatesSearch.entity().getState(), SearchCriteria.Op.IN); + IdStatesSearch.and("id", IdStatesSearch.entity().getId(), Op.EQ); + IdStatesSearch.and("states", IdStatesSearch.entity().getState(), Op.IN); IdStatesSearch.done(); - AccountDcSearch = createSearchBuilder(); - AccountDcSearch.and("account", AccountDcSearch.entity().getAccountId(), SearchCriteria.Op.EQ); - AccountDcSearch.and("dc", AccountDcSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ); - AccountDcSearch.done(); - - AccountDcRoleSearch = createSearchBuilder(); - AccountDcRoleSearch.and("account", AccountDcRoleSearch.entity().getAccountId(), SearchCriteria.Op.EQ); - AccountDcRoleSearch.and("dc", AccountDcRoleSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ); - AccountDcRoleSearch.and("role", AccountDcRoleSearch.entity().getRole(), SearchCriteria.Op.EQ); - AccountDcRoleSearch.done(); - - AccountSearch = createSearchBuilder(); - AccountSearch.and("account", AccountSearch.entity().getAccountId(), SearchCriteria.Op.EQ); - AccountSearch.done(); - - IpSearch = createSearchBuilder(); - IpSearch.and("ip", IpSearch.entity().getPublicIpAddress(), SearchCriteria.Op.EQ); - IpSearch.done(); - - HostSearch = createSearchBuilder(); - HostSearch.and("host", HostSearch.entity().getHostId(), SearchCriteria.Op.EQ); - HostSearch.done(); - - LastHostSearch = createSearchBuilder(); - LastHostSearch.and("lastHost", LastHostSearch.entity().getLastHostId(), SearchCriteria.Op.EQ); - LastHostSearch.and("state", LastHostSearch.entity().getState(), SearchCriteria.Op.EQ); - LastHostSearch.done(); - HostUpSearch = createSearchBuilder(); - HostUpSearch.and("host", HostUpSearch.entity().getHostId(), SearchCriteria.Op.EQ); - HostUpSearch.and("states", HostUpSearch.entity().getState(), SearchCriteria.Op.NIN); + HostUpSearch.and("host", HostUpSearch.entity().getHostId(), Op.EQ); + HostUpSearch.and("states", HostUpSearch.entity().getState(), Op.NIN); HostUpSearch.done(); - - DomainIdSearch = createSearchBuilder(); - DomainIdSearch.and("domainId", DomainIdSearch.entity().getDomainId(), SearchCriteria.Op.EQ); - DomainIdSearch.done(); - - StateChangeSearch = createSearchBuilder(); - StateChangeSearch.and("id", StateChangeSearch.entity().getId(), SearchCriteria.Op.EQ); - StateChangeSearch.and("states", StateChangeSearch.entity().getState(), SearchCriteria.Op.EQ); - StateChangeSearch.and("host", StateChangeSearch.entity().getHostId(), SearchCriteria.Op.EQ); - StateChangeSearch.and("update", StateChangeSearch.entity().getUpdated(), SearchCriteria.Op.EQ); - StateChangeSearch.done(); - NetworkConfigSearch = createSearchBuilder(); - NetworkConfigSearch.and("network", NetworkConfigSearch.entity().getNetworkId(), SearchCriteria.Op.EQ); - NetworkConfigSearch.and("podId", NetworkConfigSearch.entity().getPodId(), SearchCriteria.Op.EQ); - NetworkConfigSearch.done(); - - _updateTimeAttr = _allAttributes.get("updateTime"); - assert _updateTimeAttr != null : "Couldn't get this updateTime attribute"; - } - - @Override - public DomainRouterVO findByPublicIpAddress(String ipAddress) { - SearchCriteria sc = IpSearch.create(); - sc.setParameters("ip", ipAddress); - return findOneBy(sc); } @Override @@ -146,14 +83,14 @@ public class DomainRouterDaoImpl extends GenericDaoBase im @Override public List listByDataCenter(long dcId) { - SearchCriteria sc = DcSearch.create(); + SearchCriteria sc = AllFieldsSearch.create(); sc.setParameters("dc", dcId); return listBy(sc); } @Override public DomainRouterVO findBy(long accountId, long dcId) { - SearchCriteria sc = AccountDcRoleSearch.create(); + SearchCriteria sc = AllFieldsSearch.create(); sc.setParameters("account", accountId); sc.setParameters("dc", dcId); sc.setParameters("role", Role.DHCP_FIREWALL_LB_PASSWD_USERDATA); @@ -162,7 +99,7 @@ public class DomainRouterDaoImpl extends GenericDaoBase im @Override public DomainRouterVO findBy(long accountId, long dcId, Role role) { - SearchCriteria sc = AccountDcRoleSearch.create(); + SearchCriteria sc = AllFieldsSearch.create(); sc.setParameters("account", accountId); sc.setParameters("dc", dcId); sc.setParameters("role", role); @@ -171,14 +108,14 @@ public class DomainRouterDaoImpl extends GenericDaoBase im @Override public List listBy(long accountId) { - SearchCriteria sc = AccountSearch.create(); + SearchCriteria sc = AllFieldsSearch.create(); sc.setParameters("account", accountId); return listBy(sc); } @Override public List listByHostId(Long hostId) { - SearchCriteria sc = HostSearch.create(); + SearchCriteria sc = AllFieldsSearch.create(); sc.setParameters("host", hostId); return listBy(sc); } @@ -189,68 +126,45 @@ public class DomainRouterDaoImpl extends GenericDaoBase im if(hostId != null){ sc.setParameters("host", hostId); } - sc.setParameters("states", new Object[] {State.Destroyed, State.Stopped, State.Expunging}); + sc.setParameters("states", State.Destroyed, State.Stopped, State.Expunging); return listBy(sc); } - @Override - public long getNextDhcpIpAddress(long id) { - Transaction txn = Transaction.currentTxn(); - PreparedStatement pstmt = null; - try { - pstmt = txn.prepareAutoCloseStatement(GetNextDhcpAddressSql); - pstmt.setLong(1, id); - pstmt.executeUpdate(); - - pstmt = txn.prepareAutoCloseStatement(GetLastDhcpSql); - ResultSet rs = pstmt.executeQuery(); - if (rs == null || !rs.next()) { - throw new CloudRuntimeException("Unable to fetch a sequence with " + pstmt.toString()); - } - - long result = rs.getLong(1); - return result; - } catch (SQLException e) { - txn.rollback(); - s_logger.warn("DB Exception", e); - throw new CloudRuntimeException("DB Exception on " + pstmt.toString(), e); - } - } - @Override public List listByDomain(Long domainId) { - SearchCriteria sc = DomainIdSearch.create(); + SearchCriteria sc = AllFieldsSearch.create(); sc.setParameters("domainId", domainId); return listBy(sc); } @Override - public DomainRouterVO findByNetworkConfiguration(long networkConfigurationId) { - SearchCriteria sc = NetworkConfigSearch.create(); - sc.setParameters("network", networkConfigurationId); + public DomainRouterVO findByNetwork(long networkId) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("network", networkId); return findOneBy(sc); } @Override - public DomainRouterVO findByNetworkConfigurationIncludingRemoved(long networkConfigurationId) { - SearchCriteria sc = NetworkConfigSearch.create(); - sc.setParameters("network", networkConfigurationId); + public DomainRouterVO findByNetworkIncludingRemoved(long networkId) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("network", networkId); return findOneIncludingRemovedBy(sc); } @Override public List listByLastHostId(Long hostId) { - SearchCriteria sc = LastHostSearch.create(); + SearchCriteria sc = AllFieldsSearch.create(); sc.setParameters("lastHost", hostId); sc.setParameters("state", State.Stopped); return listBy(sc); } + @Override - public DomainRouterVO findByNetworkConfigurationAndPod(long networkConfigurationId, long podId) { - SearchCriteria sc = NetworkConfigSearch.create(); - sc.setParameters("network", networkConfigurationId); + public DomainRouterVO findByNetworkAndPod(long networkId, long podId) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("network", networkId); sc.setParameters("podId", podId); return findOneBy(sc); } diff --git a/utils/src/com/cloud/utils/db/GenericSearchBuilder.java b/utils/src/com/cloud/utils/db/GenericSearchBuilder.java index 5c10cbabc66..3a6cb687114 100644 --- a/utils/src/com/cloud/utils/db/GenericSearchBuilder.java +++ b/utils/src/com/cloud/utils/db/GenericSearchBuilder.java @@ -372,7 +372,13 @@ public class GenericSearchBuilder implements MethodInterceptor { } sql.append(attr.table).append(".").append(attr.columnName).append(op.toString()); - if (op.getParams() == -1) { + if (op == Op.IN && params.length == 1) { + sql.delete(sql.length() - op.toString().length(), sql.length()); + sql.append("=?"); + } else if (op == Op.NIN && params.length == 1) { + sql.delete(sql.length() - op.toString().length(), sql.length()); + sql.append("!=?"); + } else if (op.getParams() == -1) { for (int i = 0; i < params.length; i++) { sql.insert(sql.length() - 2, "?,"); }