diff --git a/agent/src/com/cloud/agent/resource/computing/FakeComputingResource.java b/agent/src/com/cloud/agent/resource/computing/FakeComputingResource.java index ad9facf7633..30fe5491c2b 100644 --- a/agent/src/com/cloud/agent/resource/computing/FakeComputingResource.java +++ b/agent/src/com/cloud/agent/resource/computing/FakeComputingResource.java @@ -556,7 +556,6 @@ public class FakeComputingResource extends ServerResourceBase implements ServerR VolumeTO vol = new VolumeTO(cmd.getVolumeId(), Volume.Type.ROOT, - Storage.StorageResourceType.STORAGE_POOL, com.cloud.storage.Storage.StoragePoolType.LVM, cmd .getPool().getUuid(), "dummy", "/mountpoint", "dummyPath", 1000L, null); diff --git a/agent/src/com/cloud/agent/resource/computing/LibvirtComputingResource.java b/agent/src/com/cloud/agent/resource/computing/LibvirtComputingResource.java index 0e02982aedc..beecedc1bbf 100644 --- a/agent/src/com/cloud/agent/resource/computing/LibvirtComputingResource.java +++ b/agent/src/com/cloud/agent/resource/computing/LibvirtComputingResource.java @@ -78,6 +78,7 @@ import com.cloud.agent.api.CleanupNetworkRulesCmd; import com.cloud.agent.api.Command; import com.cloud.agent.api.CreatePrivateTemplateFromSnapshotCommand; import com.cloud.agent.api.CreatePrivateTemplateFromVolumeCommand; +import com.cloud.agent.api.CreateStoragePoolCommand; import com.cloud.agent.api.CreateVolumeFromSnapshotAnswer; import com.cloud.agent.api.CreateVolumeFromSnapshotCommand; import com.cloud.agent.api.DeleteSnapshotBackupAnswer; diff --git a/api/src/com/cloud/api/ApiConstants.java b/api/src/com/cloud/api/ApiConstants.java index 0414a9855e5..e4de439beae 100755 --- a/api/src/com/cloud/api/ApiConstants.java +++ b/api/src/com/cloud/api/ApiConstants.java @@ -221,7 +221,7 @@ public class ApiConstants { public static final String PING_STORAGE_SERVER_IP = "pingstorageserverip"; public static final String PING_DIR = "pingdir"; public static final String TFTP_DIR = "tftpdir"; - public static final String PZING_CIFS_USERNAME = "pingcifsusername"; + public static final String PING_CIFS_USERNAME = "pingcifsusername"; public static final String PING_CIFS_PASSWORD = "pingcifspassword"; public static final String CHECKSUM="checksum"; public static final String NETWORK_DEVICE_TYPE = "networkdevicetype"; diff --git a/core/src/com/cloud/agent/resource/virtualnetwork/VirtualRoutingResource.java b/core/src/com/cloud/agent/resource/virtualnetwork/VirtualRoutingResource.java index 17b9ddebc81..579d3fcac34 100755 --- a/core/src/com/cloud/agent/resource/virtualnetwork/VirtualRoutingResource.java +++ b/core/src/com/cloud/agent/resource/virtualnetwork/VirtualRoutingResource.java @@ -520,35 +520,6 @@ public class VirtualRoutingResource implements Manager { } } - private void deletExitingLinkLocalRoutTable(String linkLocalBr) { - Script command = new Script("/bin/bash", _timeout); - command.add("-c"); - command.add("ip route | grep " + NetUtils.getLinkLocalCIDR()); - OutputInterpreter.AllLinesParser parser = new OutputInterpreter.AllLinesParser(); - String result = command.execute(parser); - boolean foundLinkLocalBr = false; - if (result == null && parser.getLines() != null) { - String[] lines = parser.getLines().split("\\n"); - for (String line : lines) { - String[] tokens = line.split(" "); - if (!tokens[2].equalsIgnoreCase(linkLocalBr)) { - Script.runSimpleBashScript("ip route del " + NetUtils.getLinkLocalCIDR()); - } else { - foundLinkLocalBr = true; - } - } - } - if (!foundLinkLocalBr) { - Script.runSimpleBashScript("ip route add " + NetUtils.getLinkLocalCIDR() + " dev " + linkLocalBr + " src " + NetUtils.getLinkLocalGateway()); - } - } - - public void createControlNetwork(String privBrName) { - deletExitingLinkLocalRoutTable(privBrName); - if (!isBridgeExists(privBrName)) { - Script.runSimpleBashScript("brctl addbr " + privBrName + "; ifconfig " + privBrName + " up;", _timeout); - } - } // protected Answer execute(final SetFirewallRuleCommand cmd) { // String args; diff --git a/server/src/com/cloud/agent/AgentManager.java b/server/src/com/cloud/agent/AgentManager.java index e26ce661ccb..ad406d8797d 100755 --- a/server/src/com/cloud/agent/AgentManager.java +++ b/server/src/com/cloud/agent/AgentManager.java @@ -142,15 +142,6 @@ public interface AgentManager extends Manager { int registerForHostEvents(Listener listener, boolean connections, boolean commands, boolean priority); - /** - * Register to listen for initial agent connections. - * @param creator - * @param priority in listening for events. - * @return id to unregister if needed. - */ - int registerForInitialConnects(StartupCommandProcessor creator, boolean priority); - - /** * Register to listen for initial agent connections. * @param creator diff --git a/server/src/com/cloud/configuration/Config.java b/server/src/com/cloud/configuration/Config.java index 7d453aec9f0..26a4a280d59 100755 --- a/server/src/com/cloud/configuration/Config.java +++ b/server/src/com/cloud/configuration/Config.java @@ -238,7 +238,6 @@ public enum Config { DefaultMaxAccountTemplates("Account Defaults", ManagementServer.class, Long.class, "max.account.templates", "20", "The default maximum number of templates that can be deployed for an account", null), DefaultMaxAccountSnapshots("Account Defaults", ManagementServer.class, Long.class, "max.account.snapshots", "20", "The default maximum number of snapshots that can be created for an account", null), DefaultMaxAccountVolumes("Account Defaults", ManagementServer.class, Long.class, "max.account.volumes", "20", "The default maximum number of volumes that can be created for an account", null); - EndpointeUrl("Advanced", ManagementServer.class, String.class, "endpointe.url", "http://localhost:8080/client/api", "Endpointe Url", null); private final String _category; private final Class _componentClass; diff --git a/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java b/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java index 308dbd1f216..ae094106606 100644 --- a/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java +++ b/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java @@ -260,10 +260,6 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx @Override public ConsoleProxyInfo assignProxy(final long dataCenterId, final long vmId) { - if (!isConsoleProxyVmRequired(dataCenterId)) { - return null; - } - final Pair result = new Pair(this, null); _requestHandlerScheduler.execute(new Runnable() { diff --git a/server/src/com/cloud/host/dao/HostDao.java b/server/src/com/cloud/host/dao/HostDao.java index 6255f64ab73..f7e4bef5d66 100644 --- a/server/src/com/cloud/host/dao/HostDao.java +++ b/server/src/com/cloud/host/dao/HostDao.java @@ -14,12 +14,11 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . * - */ -package com.cloud.host.dao; - + */ +package com.cloud.host.dao; + import java.util.Date; import java.util.List; -import java.util.Map; import com.cloud.host.Host; import com.cloud.host.Host.Type; @@ -29,115 +28,119 @@ import com.cloud.host.Status.Event; import com.cloud.hypervisor.Hypervisor.HypervisorType; import com.cloud.info.RunningHostCountInfo; import com.cloud.utils.db.GenericDao; - -/** - * Data Access Object for server - * - */ -public interface HostDao extends GenericDao { + +/** + * Data Access Object for server + * + */ +public interface HostDao extends GenericDao { List listBy(Host.Type type, Long clusterId, Long podId, long dcId); long countBy(long clusterId, Status... statuses); - - List listByDataCenter(long dcId); - List listByHostPod(long podId); - List listByStatus(Status... status); + + List listByDataCenter(long dcId); + List listByHostPod(long podId); + List listByStatus(Status... status); List listBy(Host.Type type, long dcId); - List listAllBy(Host.Type type, long dcId); - HostVO findSecondaryStorageHost(long dcId); - List listByCluster(long clusterId); - /** - * Lists all secondary storage hosts, across all zones - * @return list of Hosts - */ - List listSecondaryStorageHosts(); - - /** - * Mark all hosts in Up or Orphaned state as disconnected. This method - * is used at AgentManager startup to reset all of the connections. - * - * @param msId management server id. - * @param statuses states of the host. - */ - void markHostsAsDisconnected(long msId, Status... states); - - List findLostHosts(long timeout); - - List findHostsLike(String hostName); - - /** - * Find hosts that are directly connected. - */ - List findDirectlyConnectedHosts(); - - List findDirectAgentToLoad(long msid, long lastPingSecondsAfter, Long limit); - - /** - * Mark the host as disconnected if it is in one of these states. - * The management server id is set to null. - * The lastPinged timestamp is set to current. - * The state is set to the state passed in. - * The disconnectedOn timestamp is set to current. - * - * @param host host to be marked - * @param state state to be set to. - * @param ifStates only if it is one of these states. - * @return true if it's updated; false if not. - */ - boolean disconnect(HostVO host, Event event, long msId); - - boolean connect(HostVO host, long msId); - - HostVO findByStorageIpAddressInDataCenter(long dcId, String privateIpAddress); - HostVO findByPrivateIpAddressInDataCenter(long dcId, String privateIpAddress); - - /** - * find a host by its mac address - * @param macAddress - * @return HostVO or null if not found. - */ + List listAllBy(Host.Type type, long dcId); + HostVO findSecondaryStorageHost(long dcId); + List listByCluster(long clusterId); + /** + * Lists all secondary storage hosts, across all zones + * @return list of Hosts + */ + List listSecondaryStorageHosts(); + + /** + * Mark all hosts in Up or Orphaned state as disconnected. This method + * is used at AgentManager startup to reset all of the connections. + * + * @param msId management server id. + * @param statuses states of the host. + */ + void markHostsAsDisconnected(long msId, Status... states); + + List findLostHosts(long timeout); + + List findHostsLike(String hostName); + + /** + * Find hosts that are directly connected. + */ + List findDirectlyConnectedHosts(); + + List findDirectAgentToLoad(long msid, long lastPingSecondsAfter, Long limit); + + /** + * Mark the host as disconnected if it is in one of these states. + * The management server id is set to null. + * The lastPinged timestamp is set to current. + * The state is set to the state passed in. + * The disconnectedOn timestamp is set to current. + * + * @param host host to be marked + * @param state state to be set to. + * @param ifStates only if it is one of these states. + * @return true if it's updated; false if not. + */ + boolean disconnect(HostVO host, Event event, long msId); + + boolean connect(HostVO host, long msId); + + HostVO findByStorageIpAddressInDataCenter(long dcId, String privateIpAddress); + HostVO findByPrivateIpAddressInDataCenter(long dcId, String privateIpAddress); + + /** + * find a host by its mac address + * @param macAddress + * @return HostVO or null if not found. + */ public HostVO findByGuid(String macAddress); - - - /** - * find all hosts of a certain type in a data center - * @param type - * @param routingCapable - * @param dcId - * @return - */ - List listByTypeDataCenter(Host.Type type, long dcId); - - /** - * find all hosts of a particular type - * @param type - * @return - */ - List listByType(Type type); - - /** - * Find hosts that have not responded to a ping regardless of state - * @param timeout - * @param type - * @return - */ - List findLostHosts2(long timeout, Type type); - - /** - * update the host and changes the status depending on the Event and - * the current status. If the status changed between - * @param host host object to change - * @param event event that happened. - * @param management server who's making this update - * @return true if updated; false if not. - */ - boolean updateStatus(HostVO host, Event event, long msId); - - List getRunningHostCounts(Date cutTime); - - long getNextSequence(long hostId); - + + public HostVO findByName(String name); + + + /** + * find all hosts of a certain type in a data center + * @param type + * @param routingCapable + * @param dcId + * @return + */ + List listByTypeDataCenter(Host.Type type, long dcId); + + /** + * find all hosts of a particular type + * @param type + * @return + */ + List listByType(Type type); + + /** + * Find hosts that have not responded to a ping regardless of state + * @param timeout + * @param type + * @return + */ + List findLostHosts2(long timeout, Type type); + + /** + * update the host and changes the status depending on the Event and + * the current status. If the status changed between + * @param host host object to change + * @param event event that happened. + * @param management server who's making this update + * @return true if updated; false if not. + */ + boolean updateStatus(HostVO host, Event event, long msId); + + List getRunningHostCounts(Date cutTime); + + long getNextSequence(long hostId); + void loadDetails(HostVO host); + + void saveDetails(HostVO host); HostVO findConsoleProxyHost(String name, Type type); @@ -159,9 +162,8 @@ public interface HostDao extends GenericDao { void loadHostTags(HostVO host); List listByHostTag(Host.Type type, Long clusterId, Long podId, long dcId, String hostTag); - + long countRoutingHostsByDataCenter(long dcId); - List listSecondaryStorageHosts(long dataCenterId); - -} + List listSecondaryStorageHosts(long dataCenterId); +} diff --git a/server/src/com/cloud/host/dao/HostDaoImpl.java b/server/src/com/cloud/host/dao/HostDaoImpl.java index 8261fa2e129..725a0060d63 100644 --- a/server/src/com/cloud/host/dao/HostDaoImpl.java +++ b/server/src/com/cloud/host/dao/HostDaoImpl.java @@ -78,6 +78,7 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao protected final SearchBuilder TypeSearch; protected final SearchBuilder StatusSearch; protected final SearchBuilder NameLikeSearch; + protected final SearchBuilder NameSearch; protected final SearchBuilder SequenceSearch; protected final SearchBuilder DirectlyConnectedSearch; protected final SearchBuilder UnmanagedDirectConnectSearch; @@ -183,6 +184,10 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao StatusSearch.and("status", StatusSearch.entity().getStatus(), SearchCriteria.Op.IN); StatusSearch.done(); + NameSearch = createSearchBuilder(); + NameSearch.and("name", NameSearch.entity().getName(), SearchCriteria.Op.EQ); + NameSearch.done(); + NameLikeSearch = createSearchBuilder(); NameLikeSearch.and("name", NameLikeSearch.entity().getName(), SearchCriteria.Op.LIKE); NameLikeSearch.done(); @@ -525,6 +530,12 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao return findOneBy(sc); } + @Override + public HostVO findByName(String name) { + SearchCriteria sc = NameSearch.create("name", name); + return findOneBy(sc); + } + @Override public List findLostHosts(long timeout) { SearchCriteria sc = LastPingedSearch.create(); @@ -598,7 +609,7 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao return listBy(sc); } - protected void saveDetails(HostVO host) { + public void saveDetails(HostVO host) { Map details = host.getDetails(); if (details == null) { return; diff --git a/server/src/com/cloud/network/guru/DirectPodBasedNetworkGuru.java b/server/src/com/cloud/network/guru/DirectPodBasedNetworkGuru.java index 686153faea0..03e0b1e334f 100644 --- a/server/src/com/cloud/network/guru/DirectPodBasedNetworkGuru.java +++ b/server/src/com/cloud/network/guru/DirectPodBasedNetworkGuru.java @@ -24,8 +24,10 @@ import javax.ejb.Local; import org.apache.log4j.Logger; +import com.cloud.configuration.ZoneConfig; import com.cloud.dc.DataCenter; import com.cloud.dc.DataCenter.NetworkType; +import com.cloud.dc.DataCenterVO; import com.cloud.dc.Pod; import com.cloud.dc.Vlan; import com.cloud.dc.Vlan.VlanType; diff --git a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java index 7e28ce18fb5..3e613fbe04c 100755 --- a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java +++ b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java @@ -17,12 +17,18 @@ */ package com.cloud.network.router; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.net.URL; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.StringTokenizer; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -173,8 +179,7 @@ import com.cloud.vm.dao.UserVmDao; import com.cloud.vm.dao.VMInstanceDao; /** - * VirtualNetworkApplianceManagerImpl manages the different types of - * virtual network appliances available in the Cloud Stack. + * VirtualNetworkApplianceManagerImpl manages the different types of virtual network appliances available in the Cloud Stack. */ @Local(value = { VirtualNetworkApplianceManager.class, VirtualNetworkApplianceService.class }) public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplianceManager, VirtualNetworkApplianceService, VirtualMachineGuru { @@ -265,11 +270,11 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian NetworkDao _networkDao; @Inject LoadBalancingRulesManager _lbMgr; - @Inject + @Inject PortForwardingRulesDao _pfRulesDao; @Inject RemoteAccessVpnDao _vpnDao; - @Inject + @Inject VMInstanceDao _instanceDao; @Inject NicDao _nicDao; @@ -283,6 +288,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian int _routerStatsInterval = 300; private ServiceOfferingVO _offering; + private String trafficSentinelHostname; ScheduledExecutorService _executor; @@ -306,26 +312,26 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } @Override - public boolean destroyRouter(final long routerId) throws ResourceUnavailableException, ConcurrentOperationException{ + public boolean destroyRouter(final long routerId) throws ResourceUnavailableException, ConcurrentOperationException { UserContext context = UserContext.current(); User user = _accountMgr.getActiveUser(context.getCallerUserId()); if (s_logger.isDebugEnabled()) { s_logger.debug("Attempting to destroy router " + routerId); } - + DomainRouterVO router = _routerDao.findById(routerId); if (router == null) { return true; } boolean result = _itMgr.expunge(router, user, _accountMgr.getAccount(router.getAccountId())); - + return result; } @Override @DB - public VirtualRouter upgradeRouter(UpgradeRouterCmd cmd) throws InvalidParameterValueException, PermissionDeniedException { + public VirtualRouter upgradeRouter(UpgradeRouterCmd cmd) { Long routerId = cmd.getId(); Long serviceOfferingId = cmd.getServiceOfferingId(); Account account = UserContext.current().getCaller(); @@ -348,18 +354,19 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian if (newServiceOffering == null) { throw new InvalidParameterValueException("Unable to find service offering with id " + serviceOfferingId); } - + // Check that the router is stopped if (!router.getState().equals(State.Stopped)) { s_logger.warn("Unable to upgrade router " + router.toString() + " in state " + router.getState()); - throw new InvalidParameterValueException("Unable to upgrade router " + router.toString() + " in state " + router.getState() + "; make sure the router is stopped and not in an error state before upgrading."); + throw new InvalidParameterValueException("Unable to upgrade router " + router.toString() + " in state " + router.getState() + + "; make sure the router is stopped and not in an error state before upgrading."); } ServiceOfferingVO currentServiceOffering = _serviceOfferingDao.findById(router.getServiceOfferingId()); if (currentServiceOffering.getUseLocalStorage() != newServiceOffering.getUseLocalStorage()) { - throw new InvalidParameterValueException("Can't upgrade, due to new local storage status : " + newServiceOffering.getUseLocalStorage() - + " is different from " + "curruent local storage status: " + currentServiceOffering.getUseLocalStorage()); + throw new InvalidParameterValueException("Can't upgrade, due to new local storage status : " + newServiceOffering.getUseLocalStorage() + " is different from " + + "curruent local storage status: " + currentServiceOffering.getUseLocalStorage()); } router.setServiceOfferingId(serviceOfferingId); @@ -371,47 +378,45 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } - @Override - public boolean savePasswordToRouter(Network network, NicProfile nic, VirtualMachineProfile profile) throws ResourceUnavailableException{ + public boolean savePasswordToRouter(Network network, NicProfile nic, VirtualMachineProfile profile) throws ResourceUnavailableException { 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"); } - + UserVm userVm = profile.getVirtualMachine(); - String password = (String)profile.getParameter(Param.VmPassword); + String password = (String) profile.getParameter(Param.VmPassword); String encodedPassword = PasswordGenerator.rot13(password); - + Commands cmds = new Commands(OnError.Continue); - SavePasswordCommand cmd = new SavePasswordCommand(encodedPassword, nic.getIp4Address(), userVm.getName()); + SavePasswordCommand cmd = new SavePasswordCommand(encodedPassword, nic.getIp4Address(), userVm.getHostName()); cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, router.getPrivateIpAddress()); cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName()); cmds.addCommand("password", cmd); return sendCommandsToRouter(router, cmds); } - - + @Override public VirtualRouter stopRouter(long routerId, boolean forced) throws ResourceUnavailableException, ConcurrentOperationException { UserContext context = UserContext.current(); Account account = context.getCaller(); - + // verify parameters DomainRouterVO router = _routerDao.findById(routerId); if (router == null) { throw new InvalidParameterValueException("Unable to find router by id " + routerId + "."); } - + _accountMgr.checkAccess(account, router); - + UserVO user = _userDao.findById(UserContext.current().getCallerUserId()); - + return stop(router, forced, user, account); } - + @DB public void processStopOrRebootAnswer(final DomainRouterVO router, Answer answer) { final Transaction txn = Transaction.currentTxn(); @@ -468,24 +473,19 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } /* - * final GetVmStatsCommand cmd = new GetVmStatsCommand(router, - * router.getInstanceName()); - * final Answer answer = _agentMgr.easySend(router.getHostId(), cmd); - * if (answer == null) { - * return false; - * } + * final GetVmStatsCommand cmd = new GetVmStatsCommand(router, router.getInstanceName()); final Answer answer = + * _agentMgr.easySend(router.getHostId(), cmd); if (answer == null) { return false; } * * final GetVmStatsAnswer stats = (GetVmStatsAnswer)answer; * - * netStats.putAll(stats.getNetworkStats()); - * diskStats.putAll(stats.getDiskStats()); + * netStats.putAll(stats.getNetworkStats()); diskStats.putAll(stats.getDiskStats()); */ return true; } @Override - public VirtualRouter rebootRouter(long routerId, boolean restartNetwork) throws InvalidParameterValueException, PermissionDeniedException, ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException { + public VirtualRouter rebootRouter(long routerId, boolean restartNetwork) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException { Account caller = UserContext.current().getCaller(); // verify parameters @@ -497,16 +497,16 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian if ((caller != null) && !_domainDao.isChildDomain(caller.getDomainId(), router.getDomainId())) { throw new PermissionDeniedException("Unable to reboot domain router with id " + routerId + ". Permission denied"); } - - //Can reboot domain router only in Running state + + // Can reboot domain router only in Running state if (router == null || router.getState() != State.Running) { s_logger.warn("Unable to reboot, virtual router is not in the right state " + router.getState()); throw new ResourceUnavailableException("Unable to reboot domR, it is not in right state " + router.getState(), DataCenter.class, router.getDataCenterId()); } - + UserVO user = _userDao.findById(UserContext.current().getCallerUserId()); s_logger.debug("Stopping and starting router " + router + " as a part of router reboot"); - + if (stop(router, false, user, caller) != null) { return startRouter(routerId, restartNetwork); } else { @@ -549,11 +549,13 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian _itMgr.registerGuru(VirtualMachine.Type.DomainRouter, this); boolean useLocalStorage = Boolean.parseBoolean(configs.get(Config.SystemVMUseLocalStorage.key())); - _offering = new ServiceOfferingVO("System Offering For Software Router", 1, _routerRamSize, _routerCpuMHz, 0, 0, true, null, useLocalStorage, true, null, true); + _offering = new ServiceOfferingVO("System Offering For Software Router", 1, _routerRamSize, _routerCpuMHz, null, null, true, null, useLocalStorage, true, null, true); _offering.setUniqueName("Cloud.Com-SoftwareRouter"); _offering = _serviceOfferingDao.persistSystemServiceOffering(_offering); _systemAcct = _accountService.getSystemAccount(); + + trafficSentinelHostname = configs.get("traffic.sentinel.hostname"); s_logger.info("DomainRouterManager is configured."); @@ -588,10 +590,10 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian return VirtualMachineName.getRouterId(vmName); } - private VmDataCommand generateVmDataCommand(DomainRouterVO router, String vmPrivateIpAddress, - String userData, String serviceOffering, String zoneName, String guestIpAddress, String vmName, String vmInstanceName, long vmId, String publicKey) { + private VmDataCommand generateVmDataCommand(DomainRouterVO router, String vmPrivateIpAddress, String userData, String serviceOffering, String zoneName, String guestIpAddress, String vmName, + String vmInstanceName, long vmId, String publicKey) { VmDataCommand cmd = new VmDataCommand(vmPrivateIpAddress, vmName); - + cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, router.getPrivateIpAddress()); cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName()); @@ -606,6 +608,14 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian cmd.addVmData("metadata", "vm-id", String.valueOf(vmId)); cmd.addVmData("metadata", "public-keys", publicKey); + String cloudIdentifier = _configDao.getValue("cloud.identifier"); + if (cloudIdentifier == null) { + cloudIdentifier = ""; + } else { + cloudIdentifier = "CloudStack-{" + cloudIdentifier + "}"; + } + cmd.addVmData("metadata", "cloud-identifier", cloudIdentifier); + return cmd; } @@ -616,13 +626,74 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian @Override public void run() { + //Direct Network Usage + URL trafficSentinel; + try { + //Query traffic Sentinel + if(trafficSentinelHostname != null){ + trafficSentinel = new URL(trafficSentinelHostname+"/inmsf/Query?script=var+q+%3D+Query.topN(%22historytrmx%22,%0D%0A+++++++++++++++++%22ipsource,bytes%22,%0D%0A+++++++++++++++++%22sourcezone+!%3D+EXTERNAL" + + "+%26+destinationzone+%3D+EXTERNAL%22,%0D%0A+++++++++++++++++%22end+-+5+minutes,+end%22,%0D%0A+++++++++++++++++%22bytes%22,%0D%0A+++++++++++++++++100000);%0D%0A%0D%0Avar+totalsSent+%3D+" + + "{};%0D%0A%0D%0Avar+t+%3D+q.run(%0D%0A++function(row,table)+{%0D%0A++++if(row[0])+{++++%0D%0A++++++totalsSent[row[0]]+%3D+row[1];%0D%0A++++}%0D%0A++});%0D%0A%0D%0Avar+totalsRcvd+%3D+{};" + + "%0D%0A%0D%0Avar+q+%3D+Query.topN(%22historytrmx%22,%0D%0A+++++++++++++++++%22ipdestination,bytes%22,%0D%0A+++++++++++++++++%22destinationzone+!%3D+EXTERNAL+%26+sourcezone+%3D+EXTERNAL%22," + + "%0D%0A+++++++++++++++++%22end+-+5minutes,+end%22,%0D%0A+++++++++++++++++%22bytes%22,%0D%0A+++++++++++++++++100000);%0D%0A%0D%0Avar+t+%3D+q.run(%0D%0A++function(row,table)+{%0D%0A++++" + + "if(row[0])+{%0D%0A++++++totalsRcvd[row[0]]+%3D+row[1];%0D%0A++++}%0D%0A++});%0D%0A%0D%0Afor+(var+addr+in+totalsSent)+{%0D%0A++++var+TS+%3D+0;%0D%0A++++var+TR+%3D+0;%0D%0A++++if(totalsSent[addr])" + + "+TS+%3D+totalsSent[addr];%0D%0A++++if(totalsRcvd[addr])+TR+%3D+totalsRcvd[addr];%0D%0A++++println(addr+%2B+%22,%22+%2B+TS+%2B+%22,%22+%2B+TR);%0D%0A}&authenticate=basic&resultFormat=txt"); + + BufferedReader in = new BufferedReader( + new InputStreamReader(trafficSentinel.openStream())); + + String inputLine; + + while ((inputLine = in.readLine()) != null){ + //Parse the script output + StringTokenizer st = new StringTokenizer(inputLine, ","); + if(st.countTokens() == 3){ + String publicIp = st.nextToken(); + //Find the account owning the IP + IPAddressVO ipaddress = _ipAddressDao.findByIpAddress(publicIp); + if(ipaddress == null || ipaddress.getAccountId() == Account.ACCOUNT_ID_SYSTEM){ + continue; + } + Long bytesSent = new Long(st.nextToken()); + Long bytesRcvd = new Long(st.nextToken()); + if(bytesSent == null || bytesRcvd == null){ + s_logger.debug("Incorrect bytes for IP: "+publicIp); + } + Transaction txn = Transaction.open(Transaction.CLOUD_DB); + txn.start(); + //update user_statistics + UserStatisticsVO stats = _statsDao.lock(ipaddress.getAccountId(), ipaddress.getDataCenterId(), null, 0L, "DirectNetwork"); + if (stats == null) { + stats = new UserStatisticsVO(ipaddress.getAccountId(), ipaddress.getDataCenterId(), null, 0L, "DirectNetwork", null); + stats.setCurrentBytesSent(bytesSent); + stats.setCurrentBytesReceived(bytesRcvd); + _statsDao.persist(stats); + } else { + stats.setCurrentBytesSent(stats.getCurrentBytesSent() + bytesSent); + stats.setCurrentBytesReceived(stats.getCurrentBytesReceived() + bytesRcvd); + _statsDao.update(stats.getId(), stats); + } + txn.commit(); + txn.close(); + } + } + + in.close(); + } + } catch (MalformedURLException e1) { + s_logger.info("Invalid T raffic Sentinel URL",e1); + } catch (IOException e) { + s_logger.debug("Error in direct network usage accounting",e); + } + + final List routers = _routerDao.listUpByHostId(null); s_logger.debug("Found " + routers.size() + " running routers. "); for (DomainRouterVO router : routers) { String privateIP = router.getPrivateIpAddress(); if (privateIP != null) { - final NetworkUsageCommand usageCmd = new NetworkUsageCommand(privateIP, router.getName()); + final NetworkUsageCommand usageCmd = new NetworkUsageCommand(privateIP, router.getHostName()); final NetworkUsageAnswer answer = (NetworkUsageAnswer) _agentMgr.easySend(router.getHostId(), usageCmd); if (answer != null) { Transaction txn = Transaction.open(Transaction.CLOUD_DB); @@ -639,16 +710,16 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } if (stats.getCurrentBytesReceived() > answer.getBytesReceived()) { if (s_logger.isDebugEnabled()) { - s_logger.debug("Received # of bytes that's less than the last one. Assuming something went wrong and persisting it. Reported: " - + answer.getBytesReceived() + " Stored: " + stats.getCurrentBytesReceived()); + s_logger.debug("Received # of bytes that's less than the last one. Assuming something went wrong and persisting it. Reported: " + answer.getBytesReceived() + + " Stored: " + stats.getCurrentBytesReceived()); } stats.setNetBytesReceived(stats.getNetBytesReceived() + stats.getCurrentBytesReceived()); } stats.setCurrentBytesReceived(answer.getBytesReceived()); if (stats.getCurrentBytesSent() > answer.getBytesSent()) { if (s_logger.isDebugEnabled()) { - s_logger.debug("Received # of bytes that's less than the last one. Assuming something went wrong and persisting it. Reported: " - + answer.getBytesSent() + " Stored: " + stats.getCurrentBytesSent()); + s_logger.debug("Received # of bytes that's less than the last one. Assuming something went wrong and persisting it. Reported: " + answer.getBytesSent() + + " Stored: " + stats.getCurrentBytesSent()); } stats.setNetBytesSent(stats.getNetBytesSent() + stats.getCurrentBytesSent()); } @@ -657,8 +728,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian txn.commit(); } catch (Exception e) { txn.rollback(); - s_logger.warn("Unable to update user statistics for account: " + router.getAccountId() + " Rx: " - + answer.getBytesReceived() + "; Tx: " + answer.getBytesSent()); + s_logger.warn("Unable to update user statistics for account: " + router.getAccountId() + " Rx: " + answer.getBytesReceived() + "; Tx: " + answer.getBytesSent()); } finally { txn.close(); } @@ -673,6 +743,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } @Override + @DB public DomainRouterVO deployVirtualRouter(Network guestNetwork, DeployDestination dest, Account owner, Map params) throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException { long dcId = dest.getDataCenter().getId(); @@ -680,143 +751,173 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian if (s_logger.isDebugEnabled()) { s_logger.debug("Starting a router for network configurations: virtual=" + guestNetwork + " in " + dest); } - - assert guestNetwork.getState() == Network.State.Implemented || guestNetwork.getState() == Network.State.Setup || guestNetwork.getState() == Network.State.Implementing: "Network is not yet fully implemented: " - + guestNetwork; - assert guestNetwork.getTrafficType() == TrafficType.Guest; - DataCenterDeployment plan = new DataCenterDeployment(dcId); + // lock guest network + Long guestNetworkId = guestNetwork.getId(); + guestNetwork = _networkDao.acquireInLockTable(guestNetworkId); - DomainRouterVO router = _routerDao.findByNetwork(guestNetwork.getId()); - if (router == null) { - long id = _routerDao.getNextInSequence(Long.class, "id"); - if (s_logger.isDebugEnabled()) { - s_logger.debug("Creating the router " + id); + if (guestNetwork == null) { + throw new ConcurrentOperationException("Unable to acquire network configuration: " + guestNetworkId); + } + + try { + + assert guestNetwork.getState() == Network.State.Implemented || guestNetwork.getState() == Network.State.Setup || guestNetwork.getState() == Network.State.Implementing : "Network is not yet fully implemented: " + + guestNetwork; + assert guestNetwork.getTrafficType() == TrafficType.Guest; + + DataCenterDeployment plan = new DataCenterDeployment(dcId); + + DomainRouterVO router = _routerDao.findByNetwork(guestNetwork.getId()); + if (router == null) { + long id = _routerDao.getNextInSequence(Long.class, "id"); + if (s_logger.isDebugEnabled()) { + s_logger.debug("Creating the router " + id); + } + + PublicIp sourceNatIp = _networkMgr.assignSourceNatIpAddress(owner, guestNetwork, _accountService.getSystemUser().getId()); + + List offerings = _networkMgr.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemControlNetwork); + NetworkOfferingVO controlOffering = offerings.get(0); + NetworkVO controlConfig = _networkMgr.setupNetwork(_systemAcct, controlOffering, plan, null, null, false, false).get(0); + + List> networks = new ArrayList>(3); + NetworkOfferingVO publicOffering = _networkMgr.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemPublicNetwork).get(0); + List publicNetworks = _networkMgr.setupNetwork(_systemAcct, publicOffering, plan, null, null, false, false); + NicProfile defaultNic = new NicProfile(); + defaultNic.setDefaultNic(true); + defaultNic.setIp4Address(sourceNatIp.getAddress().addr()); + defaultNic.setGateway(sourceNatIp.getGateway()); + defaultNic.setNetmask(sourceNatIp.getNetmask()); + defaultNic.setMacAddress(sourceNatIp.getMacAddress()); + defaultNic.setBroadcastType(BroadcastDomainType.Vlan); + defaultNic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(sourceNatIp.getVlanTag())); + defaultNic.setIsolationUri(IsolationType.Vlan.toUri(sourceNatIp.getVlanTag())); + defaultNic.setDeviceId(2); + networks.add(new Pair(publicNetworks.get(0), defaultNic)); + NicProfile gatewayNic = new NicProfile(); + gatewayNic.setIp4Address(guestNetwork.getGateway()); + gatewayNic.setBroadcastUri(guestNetwork.getBroadcastUri()); + gatewayNic.setBroadcastType(guestNetwork.getBroadcastDomainType()); + gatewayNic.setIsolationUri(guestNetwork.getBroadcastUri()); + gatewayNic.setMode(guestNetwork.getMode()); + + String gatewayCidr = guestNetwork.getCidr(); + gatewayNic.setNetmask(NetUtils.getCidrNetmask(gatewayCidr)); + networks.add(new Pair((NetworkVO) guestNetwork, gatewayNic)); + networks.add(new Pair(controlConfig, null)); + + /* Before starting router, already know the hypervisor type */ + VMTemplateVO template = _templateDao.findRoutingTemplate(dest.getCluster().getHypervisorType()); + router = new DomainRouterVO(id, _offering.getId(), VirtualMachineName.getRouterName(id, _instance), template.getId(), template.getHypervisorType(), template.getGuestOSId(), + owner.getDomainId(), owner.getId(), guestNetwork.getId(), _offering.getOfferHA()); + router = _itMgr.allocate(router, template, _offering, networks, plan, null, owner); } - PublicIp sourceNatIp = _networkMgr.assignSourceNatIpAddress(owner, guestNetwork, _accountService.getSystemUser().getId()); - - List offerings = _networkMgr.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemControlNetwork); - NetworkOfferingVO controlOffering = offerings.get(0); - NetworkVO controlConfig = _networkMgr.setupNetwork(_systemAcct, controlOffering, plan, null, null, false, false).get(0); - - List> networks = new ArrayList>(3); - NetworkOfferingVO publicOffering = _networkMgr.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemPublicNetwork).get(0); - List publicNetworks = _networkMgr.setupNetwork(_systemAcct, publicOffering, plan, null, null, false, false); - NicProfile defaultNic = new NicProfile(); - defaultNic.setDefaultNic(true); - defaultNic.setIp4Address(sourceNatIp.getAddress().addr()); - defaultNic.setGateway(sourceNatIp.getGateway()); - defaultNic.setNetmask(sourceNatIp.getNetmask()); - defaultNic.setMacAddress(sourceNatIp.getMacAddress()); - defaultNic.setBroadcastType(BroadcastDomainType.Vlan); - defaultNic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(sourceNatIp.getVlanTag())); - defaultNic.setIsolationUri(IsolationType.Vlan.toUri(sourceNatIp.getVlanTag())); - defaultNic.setDeviceId(2); - networks.add(new Pair(publicNetworks.get(0), defaultNic)); - NicProfile gatewayNic = new NicProfile(); - gatewayNic.setIp4Address(guestNetwork.getGateway()); - gatewayNic.setBroadcastUri(guestNetwork.getBroadcastUri()); - gatewayNic.setBroadcastType(guestNetwork.getBroadcastDomainType()); - gatewayNic.setIsolationUri(guestNetwork.getBroadcastUri()); - gatewayNic.setMode(guestNetwork.getMode()); - - String gatewayCidr = guestNetwork.getCidr(); - gatewayNic.setNetmask(NetUtils.getCidrNetmask(gatewayCidr)); - networks.add(new Pair((NetworkVO) guestNetwork, gatewayNic)); - networks.add(new Pair(controlConfig, null)); - - /*Before starting router, already know the hypervisor type*/ - VMTemplateVO template = _templateDao.findRoutingTemplate(dest.getCluster().getHypervisorType()); - router = new DomainRouterVO(id, _offering.getId(), VirtualMachineName.getRouterName(id, _instance), template.getId(), - template.getHypervisorType(), template.getGuestOSId(), owner.getDomainId(), owner.getId(), guestNetwork.getId(), _offering.getOfferHA()); - router = _itMgr.allocate(router, template, _offering, networks, plan, null, owner); - } - - State state = router.getState(); - if (state != State.Running) { - router = this.start(router, _accountService.getSystemUser(), _accountService.getSystemAccount(), params); - } - - // Creating stats entry for router - UserStatisticsVO stats = _userStatsDao.findBy(owner.getId(), dcId, null, router.getId(), router.getType().toString()); - if (stats == null) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Creating user statistics for the account: " + owner.getId() + " Router Id: "+router.getId()); + State state = router.getState(); + if (state != State.Running) { + router = this.start(router, _accountService.getSystemUser(), _accountService.getSystemAccount(), params); } - stats = new UserStatisticsVO(owner.getId(), dcId, null, router.getId(), router.getType().toString(), guestNetwork.getId()); - _userStatsDao.persist(stats); + + // Creating stats entry for router + UserStatisticsVO stats = _userStatsDao.findBy(owner.getId(), dcId, null, router.getId(), router.getType().toString()); + if (stats == null) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Creating user statistics for the account: " + owner.getId() + " Router Id: " + router.getId()); + } + stats = new UserStatisticsVO(owner.getId(), dcId, null, router.getId(), router.getType().toString(), guestNetwork.getId()); + _userStatsDao.persist(stats); + } + return router; + } finally { + _networkDao.releaseFromLockTable(guestNetworkId); } - return router; } @Override - public DomainRouterVO deployDhcp(Network guestNetwork, DeployDestination dest, Account owner, Map params) throws InsufficientCapacityException, - StorageUnavailableException, ConcurrentOperationException, ResourceUnavailableException { + @DB + public DomainRouterVO deployDhcp(Network guestNetwork, DeployDestination dest, Account owner, Map params) throws InsufficientCapacityException, StorageUnavailableException, + ConcurrentOperationException, ResourceUnavailableException { long dcId = dest.getDataCenter().getId(); - - NetworkOffering offering = _networkOfferingDao.findByIdIncludingRemoved(guestNetwork.getNetworkOfferingId()); - if (offering.isSystemOnly() || guestNetwork.getIsShared()) { - owner = _accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM); + + // lock guest network + Long guestNetworkId = guestNetwork.getId(); + guestNetwork = _networkDao.acquireInLockTable(guestNetworkId); + + if (guestNetwork == null) { + throw new ConcurrentOperationException("Unable to acquire network configuration: " + guestNetworkId); } - if (s_logger.isDebugEnabled()) { - s_logger.debug("Starting a dhcp for network configurations: dhcp=" + guestNetwork + " in " + dest); - } - assert guestNetwork.getState() == Network.State.Implemented || guestNetwork.getState() == Network.State.Setup || guestNetwork.getState() == Network.State.Implementing : "Network is not yet fully implemented: " - + guestNetwork; + try { - DataCenterDeployment plan = new DataCenterDeployment(dcId); - DataCenter dc = _dcDao.findById(dcId); - DomainRouterVO router = null; - Long podId = dest.getPod().getId(); - - //In Basic zone and Guest network we have to start domR per pod, not per network - if ((dc.getNetworkType() == NetworkType.Basic || guestNetwork.isSecurityGroupEnabled()) && guestNetwork.getTrafficType() == TrafficType.Guest ) { - router = _routerDao.findByNetworkAndPod(guestNetwork.getId(), podId); - } else { - router = _routerDao.findByNetwork(guestNetwork.getId()); - } - - if (router == null) { - long id = _routerDao.getNextInSequence(Long.class, "id"); - if (s_logger.isDebugEnabled()) { - s_logger.debug("Creating the router " + id); + NetworkOffering offering = _networkOfferingDao.findByIdIncludingRemoved(guestNetwork.getNetworkOfferingId()); + if (offering.isSystemOnly() || guestNetwork.getIsShared()) { + owner = _accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM); } - List offerings = _networkMgr.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemControlNetwork); - NetworkOfferingVO controlOffering = offerings.get(0); - NetworkVO controlConfig = _networkMgr.setupNetwork(_systemAcct, controlOffering, plan, null, null, false, false).get(0); - - List> networks = new ArrayList>(3); - NicProfile gatewayNic = new NicProfile(); - gatewayNic.setDefaultNic(true); - networks.add(new Pair((NetworkVO) guestNetwork, gatewayNic)); - networks.add(new Pair(controlConfig, null)); - - /*Before starting router, already know the hypervisor type*/ - VMTemplateVO template = _templateDao.findRoutingTemplate(dest.getCluster().getHypervisorType()); - - router = new DomainRouterVO(id, _offering.getId(), VirtualMachineName.getRouterName(id, _instance), template.getId(), - template.getHypervisorType(), template.getGuestOSId(), owner.getDomainId(), owner.getId(), guestNetwork.getId(), _offering.getOfferHA()); - router.setRole(Role.DHCP_USERDATA); - router = _itMgr.allocate(router, template, _offering, networks, plan, null, owner); - } - State state = router.getState(); - if (state != State.Running) { - router = this.start(router, _accountService.getSystemUser(), _accountService.getSystemAccount(), params); - } - // Creating stats entry for router - UserStatisticsVO stats = _userStatsDao.findBy(owner.getId(), dcId, null, router.getId(), router.getType().toString()); - if (stats == null) { if (s_logger.isDebugEnabled()) { - s_logger.debug("Creating user statistics for the account: " + owner.getId() + " Router Id: "+router.getId()); + s_logger.debug("Starting a dhcp for network configurations: dhcp=" + guestNetwork + " in " + dest); } - stats = new UserStatisticsVO(owner.getId(), dcId, null, router.getId(), router.getType().toString(), guestNetwork.getId()); - _userStatsDao.persist(stats); - } + assert guestNetwork.getState() == Network.State.Implemented || guestNetwork.getState() == Network.State.Setup || guestNetwork.getState() == Network.State.Implementing : "Network is not yet fully implemented: " + + guestNetwork; - return router; + DataCenterDeployment plan = null; + DataCenter dc = _dcDao.findById(dcId); + DomainRouterVO router = null; + Long podId = dest.getPod().getId(); + + // In Basic zone and Guest network we have to start domR per pod, not per network + if ((dc.getNetworkType() == NetworkType.Basic || guestNetwork.isSecurityGroupEnabled()) && guestNetwork.getTrafficType() == TrafficType.Guest) { + router = _routerDao.findByNetworkAndPod(guestNetwork.getId(), podId); + plan = new DataCenterDeployment(dcId, podId, null, null, null); + } else { + router = _routerDao.findByNetwork(guestNetwork.getId()); + plan = new DataCenterDeployment(dcId); + } + + if (router == null) { + long id = _routerDao.getNextInSequence(Long.class, "id"); + if (s_logger.isDebugEnabled()) { + s_logger.debug("Creating the router " + id); + } + + List offerings = _networkMgr.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemControlNetwork); + NetworkOfferingVO controlOffering = offerings.get(0); + NetworkVO controlConfig = _networkMgr.setupNetwork(_systemAcct, controlOffering, plan, null, null, false, false).get(0); + + List> networks = new ArrayList>(3); + NicProfile gatewayNic = new NicProfile(); + gatewayNic.setDefaultNic(true); + networks.add(new Pair((NetworkVO) guestNetwork, gatewayNic)); + networks.add(new Pair(controlConfig, null)); + + /* Before starting router, already know the hypervisor type */ + VMTemplateVO template = _templateDao.findRoutingTemplate(dest.getCluster().getHypervisorType()); + + router = new DomainRouterVO(id, _offering.getId(), VirtualMachineName.getRouterName(id, _instance), template.getId(), template.getHypervisorType(), template.getGuestOSId(), + owner.getDomainId(), owner.getId(), guestNetwork.getId(), _offering.getOfferHA()); + router.setRole(Role.DHCP_USERDATA); + router = _itMgr.allocate(router, template, _offering, networks, plan, null, owner); + } + + State state = router.getState(); + if (state != State.Running) { + router = this.start(router, _accountService.getSystemUser(), _accountService.getSystemAccount(), params); + } + // Creating stats entry for router + UserStatisticsVO stats = _userStatsDao.findBy(owner.getId(), dcId, null, router.getId(), router.getType().toString()); + if (stats == null) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Creating user statistics for the account: " + owner.getId() + " Router Id: " + router.getId()); + } + stats = new UserStatisticsVO(owner.getId(), dcId, null, router.getId(), router.getType().toString(), guestNetwork.getId()); + _userStatsDao.persist(stats); + } + + return router; + } finally { + _networkDao.releaseFromLockTable(guestNetworkId); + } } @Override @@ -829,14 +930,14 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian String dhcpRange = null; DataCenter dc = dest.getDataCenter(); - + if (dc.getNetworkType() == NetworkType.Advanced) { String cidr = network.getCidr(); if (cidr != null) { dhcpRange = NetUtils.getDhcpRange(cidr); } - } - + } + if (router.getRole() == Role.DHCP_USERDATA) { type = "dhcpsrvr"; } else { @@ -855,44 +956,50 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian buf.append(" eth").append(deviceId).append("ip=").append(nic.getIp4Address()); buf.append(" eth").append(deviceId).append("mask=").append(nic.getNetmask()); if (nic.isDefaultNic()) { - buf.append(" gateway=").append(nic.getGateway()); + buf.append(" gateway=").append(nic.getGateway()); defaultDns1 = nic.getDns1(); defaultDns2 = nic.getDns2(); - + if (dc.getNetworkType() == NetworkType.Basic) { long cidrSize = NetUtils.getCidrSize(nic.getNetmask()); String cidr = NetUtils.getCidrSubNet(nic.getGateway(), cidrSize); if (cidr != null) { dhcpRange = NetUtils.getIpRangeStartIpFromCidr(cidr, cidrSize); } - } + } } if (nic.getTrafficType() == TrafficType.Management) { buf.append(" localgw=").append(dest.getPod().getGateway()); } else if (nic.getTrafficType() == TrafficType.Control) { - + // DOMR control command is sent over management server in VMware if (dest.getHost().getHypervisorType() == HypervisorType.VMware) { - if(s_logger.isInfoEnabled()) { + if (s_logger.isInfoEnabled()) { s_logger.info("Check if we need to add management server explicit route to DomR. pod cidr: " + dest.getPod().getCidrAddress() + "/" + dest.getPod().getCidrSize() - + ", pod gateway: " + dest.getPod().getGateway() + ", management host: " + _mgmt_host); + + ", pod gateway: " + dest.getPod().getGateway() + ", management host: " + _mgmt_host); } - - if(!NetUtils.sameSubnetCIDR(_mgmt_host, dest.getPod().getGateway(), dest.getPod().getCidrSize())) { - if(s_logger.isInfoEnabled()) { - s_logger.info("Add management server explicit route to DomR."); - } - - _mgmt_cidr = _configDao.getValue(Config.ManagementNetwork.key()); - if (NetUtils.isValidCIDR(_mgmt_cidr)) { - buf.append(" mgmtcidr=").append(_mgmt_cidr); - buf.append(" localgw=").append(dest.getPod().getGateway()); - } - } else { - if(s_logger.isInfoEnabled()) { - s_logger.info("Management server host is at same subnet at pod private network, don't add explict route to DomR"); - } - } + + if (s_logger.isInfoEnabled()) { + s_logger.info("Add management server explicit route to DomR."); + } + + // always add management explicit route, for basic networking setup, DomR may have two interfaces while both + // are on the same subnet + _mgmt_cidr = _configDao.getValue(Config.ManagementNetwork.key()); + if (NetUtils.isValidCIDR(_mgmt_cidr)) { + buf.append(" mgmtcidr=").append(_mgmt_cidr); + buf.append(" localgw=").append(dest.getPod().getGateway()); + } + + /* + * if(!NetUtils.sameSubnetCIDR(_mgmt_host, dest.getPod().getGateway(), dest.getPod().getCidrSize())) { + * if(s_logger.isInfoEnabled()) { s_logger.info("Add management server explicit route to DomR."); } + * + * _mgmt_cidr = _configDao.getValue(Config.ManagementNetwork.key()); if (NetUtils.isValidCIDR(_mgmt_cidr)) { + * buf.append(" mgmtcidr=").append(_mgmt_cidr); buf.append(" localgw=").append(dest.getPod().getGateway()); + * } } else { if(s_logger.isInfoEnabled()) { + * s_logger.info("Management server host is at same subnet at pod private network"); } } + */ } controlNic = nic; @@ -905,11 +1012,11 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian String domain = network.getNetworkDomain(); if (domain != null) { buf.append(" domain=" + domain); - } - + } + if (!network.isDefault() && network.getGuestType() == GuestIpType.Direct) { buf.append(" defaultroute=false"); - + String virtualNetworkElementNicIP = _networkMgr.getIpOfNetworkElementInVirtualNetwork(network.getAccountId(), network.getDataCenterId()); if (!network.getIsShared() && virtualNetworkElementNicIP != null) { defaultDns1 = virtualNetworkElementNicIP; @@ -919,7 +1026,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } else { buf.append(" defaultroute=true"); } - + buf.append(" dns1=").append(defaultDns1); if (defaultDns2 != null) { buf.append(" dns2=").append(defaultDns2); @@ -937,79 +1044,78 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } @Override - public boolean finalizeDeployment(Commands cmds, VirtualMachineProfile profile, DeployDestination dest, ReservationContext context) throws ResourceUnavailableException{ + public boolean finalizeDeployment(Commands cmds, VirtualMachineProfile profile, DeployDestination dest, ReservationContext context) throws ResourceUnavailableException { DomainRouterVO router = profile.getVirtualMachine(); List nics = profile.getNics(); for (NicProfile nic : nics) { - if (nic.getTrafficType() == TrafficType.Public) { - router.setPublicIpAddress(nic.getIp4Address()); - router.setPublicNetmask(nic.getNetmask()); - router.setPublicMacAddress(nic.getMacAddress()); - } else if (nic.getTrafficType() == TrafficType.Guest) { - router.setGuestIpAddress(nic.getIp4Address()); - } else if (nic.getTrafficType() == TrafficType.Control) { - router.setPrivateIpAddress(nic.getIp4Address()); - router.setPrivateMacAddress(nic.getMacAddress()); - } + if (nic.getTrafficType() == TrafficType.Public) { + router.setPublicIpAddress(nic.getIp4Address()); + router.setPublicNetmask(nic.getNetmask()); + router.setPublicMacAddress(nic.getMacAddress()); + } else if (nic.getTrafficType() == TrafficType.Guest) { + router.setGuestIpAddress(nic.getIp4Address()); + } else if (nic.getTrafficType() == TrafficType.Control) { + router.setPrivateIpAddress(nic.getIp4Address()); + router.setPrivateMacAddress(nic.getMacAddress()); + } } _routerDao.update(router.getId(), router); - + finalizeCommandsOnStart(cmds, profile); return true; } - + @Override public boolean finalizeCommandsOnStart(Commands cmds, VirtualMachineProfile profile) { - DomainRouterVO router = profile.getVirtualMachine(); - - NicProfile controlNic = null; - for (NicProfile nic : profile.getNics()) { - if (nic.getTrafficType() == TrafficType.Control && nic.getIp4Address() != null) { - controlNic = nic; - } - } + DomainRouterVO router = profile.getVirtualMachine(); - if (controlNic == null) { - s_logger.error("Control network doesn't exist for the router " + router); - return false; - } - - cmds.addCommand("checkSsh", new CheckSshCommand(profile.getInstanceName(), controlNic.getIp4Address(), 3922, 5, 20)); - - //restart network if restartNetwork = false is not specified in profile parameters + NicProfile controlNic = null; + for (NicProfile nic : profile.getNics()) { + if (nic.getTrafficType() == TrafficType.Control && nic.getIp4Address() != null) { + controlNic = nic; + } + } + + if (controlNic == null) { + s_logger.error("Control network doesn't exist for the router " + router); + return false; + } + + cmds.addCommand("checkSsh", new CheckSshCommand(profile.getInstanceName(), controlNic.getIp4Address(), 3922, 5, 20)); + + // restart network if restartNetwork = false is not specified in profile parameters boolean restartNetwork = true; - if (profile.getParameter(Param.RestartNetwork) != null && (Boolean)profile.getParameter(Param.RestartNetwork) == false) { + if (profile.getParameter(Param.RestartNetwork) != null && (Boolean) profile.getParameter(Param.RestartNetwork) == false) { restartNetwork = false; } - //The commands should be sent for domR only, skip for DHCP + // The commands should be sent for domR only, skip for DHCP if (router.getRole() == VirtualRouter.Role.DHCP_FIREWALL_LB_PASSWD_USERDATA && restartNetwork) { s_logger.debug("Resending ipAssoc, port forwarding, load balancing rules as a part of Virtual router start"); long networkId = router.getNetworkId(); long ownerId = router.getAccountId(); long zoneId = router.getDataCenterId(); - - + final List userIps = _networkMgr.listPublicIpAddressesInVirtualNetwork(ownerId, zoneId, null, null); List publicIps = new ArrayList(); if (userIps != null && !userIps.isEmpty()) { for (IPAddressVO userIp : userIps) { - PublicIp publicIp = new PublicIp(userIp, _vlanDao.findById(userIp.getVlanId()), userIp.getMacAddress()); + PublicIp publicIp = new PublicIp(userIp, _vlanDao.findById(userIp.getVlanId()), NetUtils.createSequenceBasedMacAddress(userIp.getMacAddress())); publicIps.add(publicIp); } } - + s_logger.debug("Found " + publicIps.size() + " ip(s) to apply as a part of domR " + router + " start."); - - if (!publicIps.isEmpty()) { - - //Re-apply public ip addresses - should come before PF/LB/VPN - createAssociateIPCommands(router, publicIps, cmds, 0); - - List vpns = new ArrayList(); + + if (!publicIps.isEmpty()) { + + // Re-apply public ip addresses - should come before PF/LB/VPN + createAssociateIPCommands(router, publicIps, cmds, 0); + + List vpns = new ArrayList(); List pfRules = new ArrayList(); List staticNatFirewallRules = new ArrayList(); - + for (PublicIpAddress ip : publicIps) { pfRules.addAll(_pfRulesDao.listForApplication(ip.getId())); staticNatFirewallRules.addAll(_rulesDao.listByIpAndPurpose(ip.getId(), Purpose.StaticNat)); @@ -1019,32 +1125,32 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian vpns.add(vpn); } } - - //Re-apply port forwarding rules + + // Re-apply port forwarding rules s_logger.debug("Found " + pfRules.size() + " port forwarding rule(s) to apply as a part of domR " + router + " start."); if (!pfRules.isEmpty()) { createApplyPortForwardingRulesCommands(pfRules, router, cmds); - } - - //Re-apply static nat rules + } + + // Re-apply static nat rules s_logger.debug("Found " + staticNatFirewallRules.size() + " static nat rule(s) to apply as a part of domR " + router + " start."); if (!staticNatFirewallRules.isEmpty()) { List staticNatRules = new ArrayList(); for (FirewallRule rule : staticNatFirewallRules) { staticNatRules.add(_rulesMgr.buildStaticNatRule(rule)); - } + } createApplyStaticNatRulesCommands(staticNatRules, router, cmds); } - - //Re-apply vpn rules + + // Re-apply vpn rules s_logger.debug("Found " + vpns.size() + " vpn(s) to apply as a part of domR " + router + " start."); if (!vpns.isEmpty()) { for (RemoteAccessVpn vpn : vpns) { createApplyVpnCommands(vpn, router, cmds); } - } - - //Re-apply load balancing rules + } + + // Re-apply load balancing rules List lbs = _loadBalancerDao.listByNetworkId(networkId); List lbRules = new ArrayList(); for (LoadBalancerVO lb : lbs) { @@ -1052,24 +1158,24 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian LoadBalancingRule loadBalancing = new LoadBalancingRule(lb, dstList); lbRules.add(loadBalancing); } - + s_logger.debug("Found " + lbRules.size() + " load balancing rule(s) to apply as a part of domR " + router + " start."); if (!lbRules.isEmpty()) { createApplyLoadBalancingRulesCommands(lbRules, router, cmds); - } - } + } + } } - - //Resend dhcp + + // Resend dhcp s_logger.debug("Reapplying dhcp entries as a part of domR " + router + " start..."); createDhcpEntriesCommands(router, cmds); - - //Resend user data + + // Resend user data s_logger.debug("Reapplying vm data (userData and metaData) entries as a part of domR " + router + " start..."); createVmDataCommands(router, cmds); // Network usage command to create iptables rules - cmds.addCommand("networkUsage", new NetworkUsageCommand(controlNic.getIp4Address(), router.getName(), "create")); - + cmds.addCommand("networkUsage", new NetworkUsageCommand(controlNic.getIp4Address(), router.getHostName(), "create")); + return true; } @@ -1080,7 +1186,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian s_logger.warn("Unable to ssh to the VM: " + answer.getDetails()); return false; } - + return true; } @@ -1092,26 +1198,26 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian processStopOrRebootAnswer(domR, answer); } } - + @Override public void finalizeExpunge(DomainRouterVO vm) { } @Override public boolean startRemoteAccessVpn(Network network, RemoteAccessVpn vpn) throws ResourceUnavailableException { - + 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()); + throw new ResourceUnavailableException("Failed to start remote access VPN: no router found for account and zone", DataCenter.class, network.getDataCenterId()); } if (router.getState() != State.Running) { - s_logger.warn("Failed to start remote access VPN: 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()); + s_logger.warn("Failed to start remote access VPN: router not in right state " + router.getState()); + throw new ResourceUnavailableException("Failed to start remote access VPN: router not in right state " + router.getState(), DataCenter.class, network.getDataCenterId()); } - + Commands cmds = new Commands(OnError.Stop); - + createApplyVpnCommands(vpn, router, cmds); try { @@ -1122,43 +1228,42 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } Answer answer = cmds.getAnswer("users"); if (!answer.getResult()) { - s_logger.error("Unable to start vpn: unable add users to vpn in zone " + router.getDataCenterId() + " for account " + vpn.getAccountId() - + " on domR: " + router.getInstanceName() + " due to " + answer.getDetails()); - throw new ResourceUnavailableException("Unable to start vpn: Unable to add users to vpn in zone " + router.getDataCenterId() + " for account " - + vpn.getAccountId() + " on domR: " + router.getInstanceName() + " due to " + answer.getDetails(), DataCenter.class, - router.getDataCenterId()); + s_logger.error("Unable to start vpn: unable add users to vpn in zone " + router.getDataCenterId() + " for account " + vpn.getAccountId() + " on domR: " + router.getInstanceName() + + " due to " + answer.getDetails()); + throw new ResourceUnavailableException("Unable to start vpn: Unable to add users to vpn in zone " + router.getDataCenterId() + " for account " + vpn.getAccountId() + " on domR: " + + router.getInstanceName() + " due to " + answer.getDetails(), DataCenter.class, router.getDataCenterId()); } answer = cmds.getAnswer("startVpn"); if (!answer.getResult()) { - s_logger.error("Unable to start vpn in zone " + router.getDataCenterId() + " for account " + vpn.getAccountId() + " on domR: " - + router.getInstanceName() + " due to " + answer.getDetails()); - throw new ResourceUnavailableException("Unable to start vpn in zone " + router.getDataCenterId() + " for account " + vpn.getAccountId() - + " on domR: " + router.getInstanceName() + " due to " + answer.getDetails(), DataCenter.class, router.getDataCenterId()); + s_logger.error("Unable to start vpn in zone " + router.getDataCenterId() + " for account " + vpn.getAccountId() + " on domR: " + router.getInstanceName() + " due to " + + answer.getDetails()); + throw new ResourceUnavailableException("Unable to start vpn in zone " + router.getDataCenterId() + " for account " + vpn.getAccountId() + " on domR: " + router.getInstanceName() + + " due to " + answer.getDetails(), DataCenter.class, router.getDataCenterId()); } return true; } @Override - public boolean deleteRemoteAccessVpn(Network network, RemoteAccessVpn vpn) throws ResourceUnavailableException{ - + public boolean deleteRemoteAccessVpn(Network network, RemoteAccessVpn vpn) throws ResourceUnavailableException { + DomainRouterVO router = getRouter(vpn.getAccountId(), network.getDataCenterId()); if (router == null) { s_logger.warn("Failed to delete remote access VPN: no router found for account and zone"); - throw new ResourceUnavailableException("Unable to apply lb rules", DataCenter.class, network.getDataCenterId()); + throw new ResourceUnavailableException("Failed to delete remote access VPN", DataCenter.class, network.getDataCenterId()); } if (router.getState() != State.Running) { - s_logger.warn("Failed to delete remote access VPN: router not in running state"); + s_logger.warn("Failed to delete remote access VPN: domR is not in right state " + router.getState()); throw new ResourceUnavailableException("Failed to delete remote access VPN: domR is not in right state " + router.getState(), DataCenter.class, network.getDataCenterId()); } - Commands cmds = new Commands(OnError.Continue); - IpAddress ip = _networkMgr.getIp(vpn.getServerAddressId()); - - RemoteAccessVpnCfgCommand removeVpnCmd = new RemoteAccessVpnCfgCommand(false, ip.getAddress().addr(), vpn.getLocalIp(), vpn.getIpRange(), vpn.getIpsecPresharedKey()); - removeVpnCmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, router.getPrivateIpAddress()); - removeVpnCmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName()); - cmds.addCommand(removeVpnCmd); - - return sendCommandsToRouter(router, cmds); + Commands cmds = new Commands(OnError.Continue); + IpAddress ip = _networkMgr.getIp(vpn.getServerAddressId()); + + RemoteAccessVpnCfgCommand removeVpnCmd = new RemoteAccessVpnCfgCommand(false, ip.getAddress().addr(), vpn.getLocalIp(), vpn.getIpRange(), vpn.getIpsecPresharedKey()); + removeVpnCmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, router.getPrivateIpAddress()); + removeVpnCmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName()); + cmds.addCommand(removeVpnCmd); + + return sendCommandsToRouter(router, cmds); } private DomainRouterVO start(DomainRouterVO router, User user, Account caller, Map params) throws StorageUnavailableException, InsufficientCapacityException, @@ -1170,31 +1275,30 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian return null; } } - + @Override public DomainRouterVO stop(VirtualRouter router, boolean forced, User user, Account caller) throws ConcurrentOperationException, ResourceUnavailableException { s_logger.debug("Stopping router " + router); try { - if (_itMgr.advanceStop((DomainRouterVO)router, forced, user, caller)) { + if (_itMgr.advanceStop((DomainRouterVO) router, forced, user, caller)) { return _routerDao.findById(router.getId()); - } else { + } else { return null; } } catch (OperationTimedoutException e) { throw new CloudRuntimeException("Unable to stop " + router, e); } } - @Override - public VirtualRouter addVirtualMachineIntoNetwork(Network network, NicProfile nic, VirtualMachineProfile profile, DeployDestination dest, - ReservationContext context, Boolean startDhcp) throws ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException { - + public VirtualRouter addVirtualMachineIntoNetwork(Network network, NicProfile nic, VirtualMachineProfile profile, DeployDestination dest, ReservationContext context, Boolean startDhcp) + throws ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException { + DomainRouterVO router = startDhcp ? deployDhcp(network, dest, profile.getOwner(), profile.getParameters()) : deployVirtualRouter(network, dest, profile.getOwner(), profile.getParameters()); _userVmDao.loadDetails((UserVmVO) profile.getVirtualMachine()); - - String password = (String)profile.getParameter(VirtualMachineProfile.Param.VmPassword); + + String password = (String) profile.getParameter(VirtualMachineProfile.Param.VmPassword); String userData = profile.getVirtualMachine().getUserData(); String sshPublicKey = profile.getVirtualMachine().getDetail("SSH.PublicKey"); Commands cmds = new Commands(OnError.Stop); @@ -1207,17 +1311,16 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian routerControlIpAddress = n.getIp4Address(); } } - - DhcpEntryCommand dhcpCommand = new DhcpEntryCommand(nic.getMacAddress(), nic.getIp4Address(), profile.getVirtualMachine() - .getName()); + + DhcpEntryCommand dhcpCommand = new DhcpEntryCommand(nic.getMacAddress(), nic.getIp4Address(), profile.getVirtualMachine().getHostName()); dhcpCommand.setAccessDetail(NetworkElementCommand.ROUTER_IP, routerControlIpAddress); dhcpCommand.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName()); cmds.addCommand("dhcp", dhcpCommand); - //password should be set only on default network element + // password should be set only on default network element if (password != null && network.isDefault()) { final String encodedPassword = PasswordGenerator.rot13(password); - SavePasswordCommand cmd = new SavePasswordCommand(encodedPassword, nic.getIp4Address(), profile.getVirtualMachine().getName()); + SavePasswordCommand cmd = new SavePasswordCommand(encodedPassword, nic.getIp4Address(), profile.getVirtualMachine().getHostName()); cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, router.getPrivateIpAddress()); cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName()); cmds.addCommand("password", cmd); @@ -1225,11 +1328,11 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian String serviceOffering = _serviceOfferingDao.findByIdIncludingRemoved(profile.getServiceOfferingId()).getDisplayText(); String zoneName = _dcDao.findById(network.getDataCenterId()).getName(); - + cmds.addCommand( "vmdata", - generateVmDataCommand(router, nic.getIp4Address(), userData, serviceOffering, zoneName, - nic.getIp4Address(), profile.getVirtualMachine().getName(), profile.getVirtualMachine().getInstanceName(), profile.getId(), sshPublicKey)); + generateVmDataCommand(router, nic.getIp4Address(), userData, serviceOffering, zoneName, nic.getIp4Address(), profile.getVirtualMachine().getHostName(), profile.getVirtualMachine() + .getInstanceName(), profile.getId(), sshPublicKey)); try { _agentMgr.send(router.getHostId(), cmds); @@ -1239,9 +1342,8 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian Answer answer = cmds.getAnswer("dhcp"); if (!answer.getResult()) { - s_logger.error("Unable to set dhcp entry for " + profile + " on domR: " + router.getName() + " due to " + answer.getDetails()); - throw new ResourceUnavailableException("Unable to set dhcp entry for " + profile + " due to " + answer.getDetails(), DataCenter.class, - router.getDataCenterId()); + s_logger.error("Unable to set dhcp entry for " + profile + " on domR: " + router.getHostName() + " due to " + answer.getDetails()); + throw new ResourceUnavailableException("Unable to set dhcp entry for " + profile + " due to " + answer.getDetails(), DataCenter.class, router.getDataCenterId()); } answer = cmds.getAnswer("password"); @@ -1261,10 +1363,10 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian @Override public DomainRouterVO persist(DomainRouterVO router) { return _routerDao.persist(router); - } - + } + @Override - public String[] applyVpnUsers(Network network, List users) throws ResourceUnavailableException{ + public String[] applyVpnUsers(Network network, List users) throws ResourceUnavailableException { 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"); @@ -1274,25 +1376,27 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian s_logger.warn("Failed to add/remove VPN users: 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()); } - + Commands cmds = new Commands(OnError.Continue); List addUsers = new ArrayList(); List removeUsers = new ArrayList(); - for (VpnUser user: users) { + for (VpnUser user : users) { if (user.getState() == VpnUser.State.Add || user.getState() == VpnUser.State.Active) { addUsers.add(user); } else if (user.getState() == VpnUser.State.Revoke) { removeUsers.add(user); } } - + VpnUsersCfgCommand cmd = new VpnUsersCfgCommand(addUsers, removeUsers); cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, router.getPrivateIpAddress()); cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName()); cmds.addCommand(cmd); - - //Currently we receive just one answer from the agent. In the future we have to parse individual answers and set results accordingly - boolean agentResult = sendCommandsToRouter(router, cmds);; + + // Currently we receive just one answer from the agent. In the future we have to parse individual answers and set + // results accordingly + boolean agentResult = sendCommandsToRouter(router, cmds); + ; String[] result = new String[users.size()]; for (int i = 0; i < result.length; i++) { if (agentResult) { @@ -1301,7 +1405,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian result[i] = String.valueOf(agentResult); } } - + return result; } @@ -1330,18 +1434,18 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian throw new InvalidParameterValueException("Unable to find router by id " + routerId + "."); } _accountMgr.checkAccess(account, router); - + Account owner = _accountMgr.getAccount(router.getAccountId()); - - //Check if all networks are implemented for the domR; if not - implement them + + // Check if all networks are implemented for the domR; if not - implement them DataCenter dc = _dcDao.findById(router.getDataCenterId()); HostPodVO pod = _podDao.findById(router.getPodId()); DeployDestination dest = new DeployDestination(dc, pod, null, null); - + ReservationContext context = new ReservationContextImpl(null, null, caller, owner); - + List nics = _nicDao.listByVmId(routerId); - + for (NicVO nic : nics) { if (!_networkMgr.startNetwork(nic.getNetworkId(), dest, context)) { s_logger.warn("Failed to start network id=" + nic.getNetworkId() + " as a part of domR start"); @@ -1359,11 +1463,11 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian return this.start(router, user, account, params); } - private void createAssociateIPCommands(final DomainRouterVO router, final List ips, Commands cmds, long vmId) { - - //Ensure that in multiple vlans case we first send all ip addresses of vlan1, then all ip addresses of vlan2, etc.. + private void createAssociateIPCommands(final DomainRouterVO router, final List ips, Commands cmds, long vmId) { + + // Ensure that in multiple vlans case we first send all ip addresses of vlan1, then all ip addresses of vlan2, etc.. Map> vlanIpMap = new HashMap>(); - for (final PublicIpAddress ipAddress: ips) { + for (final PublicIpAddress ipAddress : ips) { String vlanTag = ipAddress.getVlanTag(); ArrayList ipList = vlanIpMap.get(vlanTag); if (ipList == null) { @@ -1372,48 +1476,47 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian ipList.add(ipAddress); vlanIpMap.put(vlanTag, ipList); } - - for (Map.Entry> vlanAndIp: vlanIpMap.entrySet()) { + + for (Map.Entry> vlanAndIp : vlanIpMap.entrySet()) { List ipAddrList = vlanAndIp.getValue(); - //Source nat ip address should always be sent first + // Source nat ip address should always be sent first Collections.sort(ipAddrList, new Comparator() { @Override public int compare(PublicIpAddress o1, PublicIpAddress o2) { boolean s1 = o1.isSourceNat(); boolean s2 = o2.isSourceNat(); return (s1 ^ s2) ? ((s1 ^ true) ? 1 : -1) : 0; - } }); - - IpAddressTO[] ipsToSend = new IpAddressTO[ipAddrList.size()]; - int i = 0; - boolean firstIP = true; - for (final PublicIpAddress ipAddr: ipAddrList) { - - boolean add = (ipAddr.getState() == IpAddress.State.Releasing ? false : true); - boolean sourceNat = ipAddr.isSourceNat(); - String vlanId = ipAddr.getVlanTag(); - String vlanGateway = ipAddr.getGateway(); - String vlanNetmask = ipAddr.getNetmask(); - String vifMacAddress = ipAddr.getMacAddress(); - - String vmGuestAddress = null; - - //Get network rate - required for IpAssoc - Network network = _networkMgr.getNetwork(ipAddr.getNetworkId()); - NetworkOffering no = _configMgr.getNetworkOffering(network.getNetworkOfferingId()); - Integer networkRate = _configMgr.getNetworkRate(no.getId()); - - IpAddressTO ip = new IpAddressTO(ipAddr.getAddress().addr(), add, firstIP, sourceNat, vlanId, vlanGateway, vlanNetmask, vifMacAddress, vmGuestAddress, networkRate); - ipsToSend[i++] = ip; - firstIP = false; - } - IPAssocCommand cmd = new IPAssocCommand(ipsToSend); - cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, router.getPrivateIpAddress()); - cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName()); - cmds.addCommand("IPAssocCommand", cmd); + } + }); + + IpAddressTO[] ipsToSend = new IpAddressTO[ipAddrList.size()]; + int i = 0; + boolean firstIP = true; + for (final PublicIpAddress ipAddr : ipAddrList) { + + boolean add = (ipAddr.getState() == IpAddress.State.Releasing ? false : true); + boolean sourceNat = ipAddr.isSourceNat(); + String vlanId = ipAddr.getVlanTag(); + String vlanGateway = ipAddr.getGateway(); + String vlanNetmask = ipAddr.getNetmask(); + String vifMacAddress = ipAddr.getMacAddress(); + + String vmGuestAddress = null; + + // Get network rate - required for IpAssoc + Integer networkRate = _networkMgr.getNetworkRate(ipAddr.getNetworkId(), null); + + IpAddressTO ip = new IpAddressTO(ipAddr.getAddress().addr(), add, firstIP, sourceNat, vlanId, vlanGateway, vlanNetmask, vifMacAddress, vmGuestAddress, networkRate); + ipsToSend[i++] = ip; + firstIP = false; + } + IPAssocCommand cmd = new IPAssocCommand(ipsToSend); + cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, router.getPrivateIpAddress()); + cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName()); + cmds.addCommand("IPAssocCommand", cmd); } } - + private void createApplyPortForwardingRulesCommands(List rules, DomainRouterVO router, Commands cmds) { List rulesTO = null; if (rules != null) { @@ -1424,13 +1527,13 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian rulesTO.add(ruleTO); } } - + SetPortForwardingRulesCommand cmd = new SetPortForwardingRulesCommand(rulesTO); cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, router.getPrivateIpAddress()); cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName()); cmds.addCommand(cmd); } - + private void createApplyStaticNatRulesCommands(List rules, DomainRouterVO router, Commands cmds) { List rulesTO = null; if (rules != null) { @@ -1441,23 +1544,23 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian rulesTO.add(ruleTO); } } - + SetStaticNatRulesCommand cmd = new SetStaticNatRulesCommand(rulesTO); cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, router.getPrivateIpAddress()); cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName()); cmds.addCommand(cmd); } - + private void createApplyLoadBalancingRulesCommands(List rules, DomainRouterVO router, Commands cmds) { - + LoadBalancerTO[] lbs = new LoadBalancerTO[rules.size()]; int i = 0; for (LoadBalancingRule rule : rules) { boolean revoked = (rule.getState().equals(FirewallRule.State.Revoke)); String protocol = rule.getProtocol(); String algorithm = rule.getAlgorithm(); - - String srcIp = _networkMgr.getIp(rule.getSourceIpAddressId()).getAddress().addr(); + + String srcIp = _networkMgr.getIp(rule.getSourceIpAddressId()).getAddress().addr(); int srcPort = rule.getSourcePortStart(); List destinations = rule.getDestinations(); LoadBalancerTO lb = new LoadBalancerTO(srcIp, srcPort, protocol, algorithm, revoked, false, destinations); @@ -1468,37 +1571,35 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, router.getPrivateIpAddress()); cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName()); cmds.addCommand(cmd); - + } - + private void createApplyVpnCommands(RemoteAccessVpn vpn, DomainRouterVO router, Commands cmds) { List vpnUsers = _vpnUsersDao.listByAccount(vpn.getAccountId()); List addUsers = new ArrayList(); List removeUsers = new ArrayList(); - for (VpnUser user: vpnUsers) { + for (VpnUser user : vpnUsers) { if (user.getState() == VpnUser.State.Add) { addUsers.add(user); } else if (user.getState() == VpnUser.State.Revoke) { removeUsers.add(user); } } - + VpnUsersCfgCommand addUsersCmd = new VpnUsersCfgCommand(addUsers, removeUsers); addUsersCmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, router.getPrivateIpAddress()); addUsersCmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName()); - + IpAddress ip = _networkMgr.getIp(vpn.getServerAddressId()); - - RemoteAccessVpnCfgCommand startVpnCmd = new RemoteAccessVpnCfgCommand(true, ip.getAddress().addr(), - vpn.getLocalIp(), vpn.getIpRange(), vpn.getIpsecPresharedKey()); + + RemoteAccessVpnCfgCommand startVpnCmd = new RemoteAccessVpnCfgCommand(true, ip.getAddress().addr(), vpn.getLocalIp(), vpn.getIpRange(), vpn.getIpsecPresharedKey()); startVpnCmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, router.getPrivateIpAddress()); startVpnCmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName()); - + cmds.addCommand("users", addUsersCmd); cmds.addCommand("startVpn", startVpnCmd); } - - + private void createVmDataCommands(DomainRouterVO router, Commands cmds) { long networkId = router.getNetworkId(); List vms = _userVmDao.listByNetworkId(networkId); @@ -1509,34 +1610,33 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian s_logger.debug("Creating user data entry for vm " + vm + " on domR " + router); String serviceOffering = _serviceOfferingDao.findByIdIncludingRemoved(vm.getServiceOfferingId()).getDisplayText(); String zoneName = _dcDao.findById(router.getDataCenterId()).getName(); - cmds.addCommand( - "vmdata", - generateVmDataCommand(router, nic.getIp4Address(), vm.getUserData(), serviceOffering, zoneName, - nic.getIp4Address(), vm.getName(), vm.getInstanceName(), vm.getId(), null)); - } - } - } - } - - private void createDhcpEntriesCommands(DomainRouterVO router, Commands cmds) { - long networkId = router.getNetworkId(); - List vms = _userVmDao.listByNetworkId(networkId); - if (vms != null && !vms.isEmpty()) { - for (UserVmVO vm : vms) { - NicVO nic = _nicDao.findByInstanceIdAndNetworkId(networkId, vm.getId()); - if (nic != null) { - s_logger.debug("Creating dhcp entry for vm " + vm + " on domR " + router + "."); - - DhcpEntryCommand dhcpCommand = new DhcpEntryCommand(nic.getMacAddress(), nic.getIp4Address(), vm.getName()); - dhcpCommand.setAccessDetail(NetworkElementCommand.ROUTER_IP, router.getPrivateIpAddress()); - dhcpCommand.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName()); - cmds.addCommand("dhcp", dhcpCommand); + cmds.addCommand("vmdata", + generateVmDataCommand(router, nic.getIp4Address(), vm.getUserData(), serviceOffering, zoneName, nic.getIp4Address(), vm.getHostName(), vm.getInstanceName(), vm.getId(), null)); } } } } - - + + private void createDhcpEntriesCommands(DomainRouterVO router, Commands cmds) { + long networkId = router.getNetworkId(); + List vms = _userVmDao.listByNetworkId(networkId); + if (!vms.isEmpty()) { + for (UserVmVO vm : vms) { + if (vm.getState() != State.Destroyed && vm.getState() != State.Expunging) { + NicVO nic = _nicDao.findByInstanceIdAndNetworkId(networkId, vm.getId()); + if (nic != null) { + s_logger.debug("Creating dhcp entry for vm " + vm + " on domR " + router + "."); + + DhcpEntryCommand dhcpCommand = new DhcpEntryCommand(nic.getMacAddress(), nic.getIp4Address(), vm.getHostName()); + dhcpCommand.setAccessDetail(NetworkElementCommand.ROUTER_IP, router.getPrivateIpAddress()); + dhcpCommand.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName()); + cmds.addCommand("dhcp", dhcpCommand); + } + } + } + } + } + private boolean sendCommandsToRouter(final DomainRouterVO router, Commands cmds) throws AgentUnavailableException { Answer[] answers = null; try { @@ -1572,9 +1672,9 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian if (router.getState() == State.Running) { Commands cmds = new Commands(OnError.Continue); - //Have to resend all already associated ip addresses + // Have to resend all already associated ip addresses createAssociateIPCommands(router, ipAddress, cmds, 0); - + return sendCommandsToRouter(router, cmds); } else if (router.getState() == State.Stopped) { return true; @@ -1583,19 +1683,19 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian throw new ResourceUnavailableException("Unable to assign ip addresses, domR is not in right state " + router.getState(), DataCenter.class, network.getDataCenterId()); } } - + @Override - public boolean applyFirewallRules(Network network, List rules) throws ResourceUnavailableException { + public boolean applyFirewallRules(Network network, List rules) throws ResourceUnavailableException { 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()); } - + if (router.getState() == State.Running) { if (rules != null && !rules.isEmpty()) { if (rules.get(0).getPurpose() == Purpose.LoadBalancing) { - //for load balancer we have to resend all lb rules for the network + // for load balancer we have to resend all lb rules for the network List lbs = _loadBalancerDao.listByNetworkId(network.getId()); List lbRules = new ArrayList(); for (LoadBalancerVO lb : lbs) { @@ -1603,55 +1703,53 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian LoadBalancingRule loadBalancing = new LoadBalancingRule(lb, dstList); lbRules.add(loadBalancing); } - + return applyLBRules(router, lbRules); - } else if (rules.get(0).getPurpose() == Purpose.PortForwarding) { - return applyPortForwardingRules(router, (List)rules); - } else if (rules.get(0).getPurpose() == Purpose.StaticNat) { - return applyStaticNatRules(router, (List)rules); - - }else { + } else if (rules.get(0).getPurpose() == Purpose.PortForwarding) { + return applyPortForwardingRules(router, (List) rules); + } else if (rules.get(0).getPurpose() == Purpose.StaticNat) { + return applyStaticNatRules(router, (List) rules); + + } else { s_logger.warn("Unable to apply rules of purpose: " + rules.get(0).getPurpose()); return false; } } else { return true; } - } else if (router.getState() == State.Stopped || router.getState() == State.Stopping){ + } else if (router.getState() == State.Stopped || router.getState() == State.Stopping) { s_logger.debug("Router is in " + router.getState() + ", so not sending apply firewall rules commands to the backend"); return true; } else { s_logger.warn("Unable to apply firewall rules, virtual router is not in the right state " + router.getState()); throw new ResourceUnavailableException("Unable to apply firewall rules, virtual router is not in the right state", VirtualRouter.class, router.getId()); } - } - + } protected boolean applyLBRules(DomainRouterVO router, List rules) throws ResourceUnavailableException { Commands cmds = new Commands(OnError.Continue); - createApplyLoadBalancingRulesCommands(rules, router, cmds); - //Send commands to router - return sendCommandsToRouter(router, cmds); + createApplyLoadBalancingRulesCommands(rules, router, cmds); + // Send commands to router + return sendCommandsToRouter(router, cmds); } - protected boolean applyPortForwardingRules(DomainRouterVO router, List rules) throws ResourceUnavailableException { + protected boolean applyPortForwardingRules(DomainRouterVO router, List rules) throws ResourceUnavailableException { Commands cmds = new Commands(OnError.Continue); - createApplyPortForwardingRulesCommands(rules, router, cmds); - //Send commands to router - return sendCommandsToRouter(router, cmds); + createApplyPortForwardingRulesCommands(rules, router, cmds); + // Send commands to router + return sendCommandsToRouter(router, cmds); } - - - protected boolean applyStaticNatRules(DomainRouterVO router, List rules) throws ResourceUnavailableException { + + protected boolean applyStaticNatRules(DomainRouterVO router, List rules) throws ResourceUnavailableException { Commands cmds = new Commands(OnError.Continue); - createApplyStaticNatRulesCommands(rules, router, cmds); - //Send commands to router - return sendCommandsToRouter(router, cmds); + createApplyStaticNatRulesCommands(rules, router, cmds); + // Send commands to router + return sendCommandsToRouter(router, cmds); } - + @Override public VirtualRouter getRouterForNetwork(long networkId) { return _routerDao.findByNetwork(networkId); - + } } diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java index b5ec9ef63dd..b24da40c397 100755 --- a/server/src/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/com/cloud/vm/UserVmManagerImpl.java @@ -42,10 +42,13 @@ import com.cloud.agent.api.CreatePrivateTemplateFromVolumeCommand; import com.cloud.agent.api.GetVmStatsAnswer; import com.cloud.agent.api.GetVmStatsCommand; import com.cloud.agent.api.SnapshotCommand; +import com.cloud.agent.api.StartAnswer; import com.cloud.agent.api.StopAnswer; import com.cloud.agent.api.UpgradeSnapshotCommand; import com.cloud.agent.api.VmStatsEntry; import com.cloud.agent.api.storage.CreatePrivateTemplateAnswer; +import com.cloud.agent.api.to.NicTO; +import com.cloud.agent.api.to.VirtualMachineTO; import com.cloud.agent.api.to.VolumeTO; import com.cloud.agent.manager.Commands; import com.cloud.alert.AlertManager; @@ -110,6 +113,7 @@ import com.cloud.hypervisor.Hypervisor.HypervisorType; import com.cloud.network.IPAddressVO; import com.cloud.network.Network; import com.cloud.network.Network.GuestIpType; +import com.cloud.network.Network.Provider; import com.cloud.network.NetworkManager; import com.cloud.network.NetworkVO; import com.cloud.network.Networks.TrafficType;