diff --git a/api/src/com/cloud/exception/InvalidParameterValueException.java b/api/src/com/cloud/exception/InvalidParameterValueException.java index 30b774b1163..57af94f9aad 100644 --- a/api/src/com/cloud/exception/InvalidParameterValueException.java +++ b/api/src/com/cloud/exception/InvalidParameterValueException.java @@ -25,10 +25,6 @@ public class InvalidParameterValueException extends CloudRuntimeException { private static final long serialVersionUID = -2232066904895010203L; - public InvalidParameterValueException(String message) { - super(message); - } - public InvalidParameterValueException(String message, List idProxyList) { super(message); if (idProxyList != null) { diff --git a/server/src/com/cloud/api/commands/netapp/CreateVolumeOnFilerCmd.java b/server/src/com/cloud/api/commands/netapp/CreateVolumeOnFilerCmd.java index 604d71d3a4d..ad83a9af01e 100644 --- a/server/src/com/cloud/api/commands/netapp/CreateVolumeOnFilerCmd.java +++ b/server/src/com/cloud/api/commands/netapp/CreateVolumeOnFilerCmd.java @@ -35,108 +35,108 @@ public class CreateVolumeOnFilerCmd extends BaseCmd { private static final String s_name = "createvolumeresponse"; @Parameter(name=ApiConstants.IP_ADDRESS, type=CommandType.STRING, required = true, description="ip address.") - private String ipAddress; - + private String ipAddress; + @Parameter(name=ApiConstants.AGGREGATE_NAME, type=CommandType.STRING, required = true, description="aggregate name.") - private String aggrName; + private String aggrName; @Parameter(name=ApiConstants.POOL_NAME, type=CommandType.STRING, required = true, description="pool name.") - private String poolName; - + private String poolName; + @Parameter(name=ApiConstants.VOLUME_NAME, type=CommandType.STRING, required = true, description="volume name.") - private String volName; - + private String volName; + @Parameter(name=ApiConstants.SIZE, type=CommandType.INTEGER, required = true, description="volume size.") - private Integer volSize; - + private Integer volSize; + @Parameter(name=ApiConstants.SNAPSHOT_POLICY, type=CommandType.STRING, required = false, description="snapshot policy.") - private String snapshotPolicy; - + private String snapshotPolicy; + @Parameter(name=ApiConstants.SNAPSHOT_RESERVATION, type=CommandType.INTEGER, required = false, description="snapshot reservation.") - private Integer snapshotReservation; - + private Integer snapshotReservation; + @Parameter(name=ApiConstants.USERNAME, type=CommandType.STRING, required = true, description="user name.") - private String userName; - + private String userName; + @Parameter(name=ApiConstants.PASSWORD, type=CommandType.STRING, required = true, description="password.") - private String password; - + private String password; + public String getIpAddress() { - return ipAddress; + return ipAddress; } - + public String getAggrName() { - return aggrName; + return aggrName; } - + public String getPoolName() { - return poolName; + return poolName; } - + public String volName() { - return volName; + return volName; } - + public Integer getVolSize() { - return volSize; + return volSize; } - + public String getSnapshotPolicy() { - return snapshotPolicy; + return snapshotPolicy; } - + public Integer getSnapshotReservation() { - return snapshotReservation; + return snapshotReservation; } - + public String getUserName() { - return userName; + return userName; } - + public String getPassword() { - return password; + return password; } - @Override - public void execute() throws ResourceUnavailableException, - InsufficientCapacityException, ServerApiException, - ConcurrentOperationException, ResourceAllocationException { - //param checks - if(snapshotReservation != null && (snapshotReservation<0 || snapshotReservation>100)) - throw new InvalidParameterValueException("Invalid snapshot reservation"); - - ComponentLocator locator = ComponentLocator.getLocator(ManagementService.Name); - NetappManager netappMgr = locator.getManager(NetappManager.class); - - StringBuilder s = new StringBuilder(getVolSize().toString()); - s.append("g"); - - try { - netappMgr.createVolumeOnFiler(ipAddress, aggrName, poolName, volName, s.toString(), snapshotPolicy, snapshotReservation, userName, password); - CreateVolumeOnFilerCmdResponse response = new CreateVolumeOnFilerCmdResponse(); - response.setResponseName(getCommandName()); - this.setResponseObject(response); - } catch (ServerException e) { - throw new ServerApiException(BaseCmd.INTERNAL_ERROR, e.toString()); - } catch (InvalidParameterValueException e) { - throw new ServerApiException(BaseCmd.PARAM_ERROR, e.toString()); - } catch (UnknownHostException e) { - throw new ServerApiException(BaseCmd.PARAM_ERROR, e.toString()); - } - - } + @Override + public void execute() throws ResourceUnavailableException, + InsufficientCapacityException, ServerApiException, + ConcurrentOperationException, ResourceAllocationException { + //param checks + if(snapshotReservation != null && (snapshotReservation<0 || snapshotReservation>100)) + throw new InvalidParameterValueException("Invalid snapshot reservation", null); - @Override - public String getCommandName() { - // TODO Auto-generated method stub - return s_name; - } + ComponentLocator locator = ComponentLocator.getLocator(ManagementService.Name); + NetappManager netappMgr = locator.getManager(NetappManager.class); + + StringBuilder s = new StringBuilder(getVolSize().toString()); + s.append("g"); + + try { + netappMgr.createVolumeOnFiler(ipAddress, aggrName, poolName, volName, s.toString(), snapshotPolicy, snapshotReservation, userName, password); + CreateVolumeOnFilerCmdResponse response = new CreateVolumeOnFilerCmdResponse(); + response.setResponseName(getCommandName()); + this.setResponseObject(response); + } catch (ServerException e) { + throw new ServerApiException(BaseCmd.INTERNAL_ERROR, e.toString()); + } catch (InvalidParameterValueException e) { + throw new ServerApiException(BaseCmd.PARAM_ERROR, e.toString()); + } catch (UnknownHostException e) { + throw new ServerApiException(BaseCmd.PARAM_ERROR, e.toString()); + } + + } + + @Override + public String getCommandName() { + // TODO Auto-generated method stub + return s_name; + } + + @Override + public long getEntityOwnerId() { + // TODO Auto-generated method stub + return 0; + } - @Override - public long getEntityOwnerId() { - // TODO Auto-generated method stub - return 0; - } - } diff --git a/server/src/com/cloud/configuration/DefaultComponentLibrary.java b/server/src/com/cloud/configuration/DefaultComponentLibrary.java index 19dfe89ed4c..72dcf986b2c 100755 --- a/server/src/com/cloud/configuration/DefaultComponentLibrary.java +++ b/server/src/com/cloud/configuration/DefaultComponentLibrary.java @@ -76,6 +76,7 @@ import com.cloud.network.as.dao.AutoScalePolicyConditionMapDaoImpl; import com.cloud.network.as.dao.AutoScalePolicyDaoImpl; import com.cloud.network.as.dao.AutoScaleVmGroupDaoImpl; import com.cloud.network.as.dao.AutoScaleVmGroupPolicyMapDaoImpl; +import com.cloud.network.as.dao.AutoScaleVmProfileDaoImpl; import com.cloud.network.as.dao.ConditionDaoImpl; import com.cloud.network.as.dao.CounterDaoImpl; import com.cloud.network.dao.CiscoNexusVSMDeviceDaoImpl; @@ -86,7 +87,6 @@ import com.cloud.network.dao.FirewallRulesDaoImpl; import com.cloud.network.dao.IPAddressDaoImpl; import com.cloud.network.dao.InlineLoadBalancerNicMapDaoImpl; import com.cloud.network.dao.LBStickinessPolicyDaoImpl; -import com.cloud.network.as.dao.AutoScaleVmProfileDaoImpl; import com.cloud.network.dao.LoadBalancerDaoImpl; import com.cloud.network.dao.LoadBalancerVMMapDaoImpl; import com.cloud.network.dao.NetworkDaoImpl; @@ -182,7 +182,7 @@ import com.cloud.storage.swift.SwiftManagerImpl; import com.cloud.storage.upload.UploadMonitorImpl; import com.cloud.tags.TaggedResourceManagerImpl; import com.cloud.tags.dao.ResourceTagsDaoImpl; -import com.cloud.template.HyervisorTemplateAdapter; +import com.cloud.template.HypervisorTemplateAdapter; import com.cloud.template.TemplateAdapter; import com.cloud.template.TemplateAdapter.TemplateAdapterType; import com.cloud.template.TemplateManagerImpl; @@ -439,7 +439,7 @@ public class DefaultComponentLibrary extends ComponentLibraryBase implements Com } protected void populateAdapters() { - addAdapter(TemplateAdapter.class, TemplateAdapterType.Hypervisor.getName(), HyervisorTemplateAdapter.class); + addAdapter(TemplateAdapter.class, TemplateAdapterType.Hypervisor.getName(), HypervisorTemplateAdapter.class); } @Override diff --git a/server/src/com/cloud/network/element/JuniperSRXExternalFirewallElement.java b/server/src/com/cloud/network/element/JuniperSRXExternalFirewallElement.java index 0473291d15d..73577d6a3ea 100644 --- a/server/src/com/cloud/network/element/JuniperSRXExternalFirewallElement.java +++ b/server/src/com/cloud/network/element/JuniperSRXExternalFirewallElement.java @@ -74,8 +74,8 @@ import com.cloud.network.rules.FirewallRule; import com.cloud.network.rules.PortForwardingRule; import com.cloud.offering.NetworkOffering; import com.cloud.offerings.dao.NetworkOfferingDao; -import com.cloud.resource.ServerResource; import com.cloud.server.api.response.ExternalFirewallResponse; +import com.cloud.utils.IdentityProxy; import com.cloud.utils.NumbersUtil; import com.cloud.utils.component.Inject; import com.cloud.utils.exception.CloudRuntimeException; @@ -86,7 +86,7 @@ import com.cloud.vm.VirtualMachineProfile; @Local(value = NetworkElement.class) public class JuniperSRXExternalFirewallElement extends ExternalFirewallDeviceManagerImpl implements SourceNatServiceProvider, FirewallServiceProvider, - PortForwardingServiceProvider, RemoteAccessVPNServiceProvider, IpDeployer, JuniperSRXFirewallElementService { +PortForwardingServiceProvider, RemoteAccessVPNServiceProvider, IpDeployer, JuniperSRXFirewallElementService { private static final Logger s_logger = Logger.getLogger(JuniperSRXExternalFirewallElement.class); @@ -143,7 +143,7 @@ public class JuniperSRXExternalFirewallElement extends ExternalFirewallDeviceMan @Override public boolean implement(Network network, NetworkOffering offering, DeployDestination dest, ReservationContext context) throws ResourceUnavailableException, ConcurrentOperationException, - InsufficientNetworkCapacityException { + InsufficientNetworkCapacityException { DataCenter zone = _configMgr.getZone(network.getDataCenterId()); // don't have to implement network is Basic zone @@ -168,7 +168,7 @@ public class JuniperSRXExternalFirewallElement extends ExternalFirewallDeviceMan @Override public boolean prepare(Network config, NicProfile nic, VirtualMachineProfile vm, DeployDestination dest, ReservationContext context) throws ConcurrentOperationException, - InsufficientNetworkCapacityException, ResourceUnavailableException { + InsufficientNetworkCapacityException, ResourceUnavailableException { return true; } @@ -317,7 +317,7 @@ public class JuniperSRXExternalFirewallElement extends ExternalFirewallDeviceMan @Override public boolean shutdownProviderInstances(PhysicalNetworkServiceProvider provider, ReservationContext context) throws ConcurrentOperationException, - ResourceUnavailableException { + ResourceUnavailableException { // TODO Auto-generated method stub return true; } @@ -338,18 +338,21 @@ public class JuniperSRXExternalFirewallElement extends ExternalFirewallDeviceMan zone = _dcDao.findById(zoneId); if (zone == null) { - throw new InvalidParameterValueException("Could not find zone with ID: " + zoneId); + throw new InvalidParameterValueException("Could not find zone by id", null); } List physicalNetworks = _physicalNetworkDao.listByZone(zoneId); if ((physicalNetworks == null) || (physicalNetworks.size() > 1)) { - throw new InvalidParameterValueException("There are no physical networks or multiple physical networks configured in zone with ID: " - + zoneId + " to add this device."); + List idList = new ArrayList(); + idList.add(new IdentityProxy(zone, zoneId, "zoneId")); + throw new InvalidParameterValueException("There are no physical networks or multiple " + + "physical networks configured in zone with specified zoneId" + + " to add this device.", idList); } pNetwork = physicalNetworks.get(0); String deviceType = NetworkDevice.JuniperSRXFirewall.getName(); - ExternalFirewallDeviceVO fwDeviceVO = addExternalFirewall(pNetwork.getId(), cmd.getUrl(), cmd.getUsername(), cmd.getPassword(), deviceType, (ServerResource) new JuniperSrxResource()); + ExternalFirewallDeviceVO fwDeviceVO = addExternalFirewall(pNetwork.getId(), cmd.getUrl(), cmd.getUsername(), cmd.getPassword(), deviceType, new JuniperSrxResource()); if (fwDeviceVO != null) { fwHost = _hostDao.findById(fwDeviceVO.getHostId()); } @@ -374,13 +377,16 @@ public class JuniperSRXExternalFirewallElement extends ExternalFirewallDeviceMan if (zoneId != null) { zone = _dcDao.findById(zoneId); if (zone == null) { - throw new InvalidParameterValueException("Could not find zone with ID: " + zoneId); + throw new InvalidParameterValueException("Could not find zone by id", null); } List physicalNetworks = _physicalNetworkDao.listByZone(zoneId); if ((physicalNetworks == null) || (physicalNetworks.size() > 1)) { - throw new InvalidParameterValueException("There are no physical networks or multiple physical networks configured in zone with ID: " - + zoneId + " to add this device."); + List idList = new ArrayList(); + idList.add(new IdentityProxy(zone, zoneId, "zoneId")); + throw new InvalidParameterValueException("There are no physical networks or multiple " + + "physical networks configured in zone with specified zoneId " + + "to add this device.", idList); } pNetwork = physicalNetworks.get(0); } @@ -389,6 +395,7 @@ public class JuniperSRXExternalFirewallElement extends ExternalFirewallDeviceMan return firewallHosts; } + @Override public ExternalFirewallResponse createExternalFirewallResponse(Host externalFirewall) { return super.createExternalFirewallResponse(externalFirewall); } @@ -402,10 +409,10 @@ public class JuniperSRXExternalFirewallElement extends ExternalFirewallDeviceMan public ExternalFirewallDeviceVO addSrxFirewall(AddSrxFirewallCmd cmd) { String deviceName = cmd.getDeviceType(); if (!deviceName.equalsIgnoreCase(NetworkDevice.JuniperSRXFirewall.getName())) { - throw new InvalidParameterValueException("Invalid SRX firewall device type"); + throw new InvalidParameterValueException("Invalid SRX firewall device type", null); } return addExternalFirewall(cmd.getPhysicalNetworkId(), cmd.getUrl(), cmd.getUsername(), cmd.getPassword(), deviceName, - (ServerResource) new JuniperSrxResource()); + new JuniperSrxResource()); } @Override @@ -414,7 +421,7 @@ public class JuniperSRXExternalFirewallElement extends ExternalFirewallDeviceMan ExternalFirewallDeviceVO fwDeviceVO = _fwDevicesDao.findById(fwDeviceId); if (fwDeviceVO == null || !fwDeviceVO.getDeviceName().equalsIgnoreCase(NetworkDevice.JuniperSRXFirewall.getName())) { - throw new InvalidParameterValueException("No SRX firewall device found with ID: " + fwDeviceId); + throw new InvalidParameterValueException("No SRX firewall device found by ID", null); } return deleteExternalFirewall(fwDeviceVO.getHostId()); } @@ -426,7 +433,7 @@ public class JuniperSRXExternalFirewallElement extends ExternalFirewallDeviceMan ExternalFirewallDeviceVO fwDeviceVO = _fwDevicesDao.findById(fwDeviceId); if (fwDeviceVO == null || !fwDeviceVO.getDeviceName().equalsIgnoreCase(NetworkDevice.JuniperSRXFirewall.getName())) { - throw new InvalidParameterValueException("No SRX firewall device found with ID: " + fwDeviceId); + throw new InvalidParameterValueException("No SRX firewall device found by ID", null); } if (deviceCapacity != null) { @@ -455,13 +462,13 @@ public class JuniperSRXExternalFirewallElement extends ExternalFirewallDeviceMan List fwDevices = new ArrayList(); if (physcialNetworkId == null && fwDeviceId == null) { - throw new InvalidParameterValueException("Either physical network Id or load balancer device Id must be specified"); + throw new InvalidParameterValueException("Either physical network Id or load balancer device Id must be specified", null); } if (fwDeviceId != null) { ExternalFirewallDeviceVO fwDeviceVo = _fwDevicesDao.findById(fwDeviceId); if (fwDeviceVo == null || !fwDeviceVo.getDeviceName().equalsIgnoreCase(NetworkDevice.JuniperSRXFirewall.getName())) { - throw new InvalidParameterValueException("Could not find SRX firewall device with ID: " + fwDeviceId); + throw new InvalidParameterValueException("Could not find SRX firewall device by ID", null); } fwDevices.add(fwDeviceVo); } @@ -469,7 +476,7 @@ public class JuniperSRXExternalFirewallElement extends ExternalFirewallDeviceMan if (physcialNetworkId != null) { pNetwork = _physicalNetworkDao.findById(physcialNetworkId); if (pNetwork == null) { - throw new InvalidParameterValueException("Could not find phyical network with ID: " + physcialNetworkId); + throw new InvalidParameterValueException("Could not find phyical network by ID", null); } fwDevices = _fwDevicesDao.listByPhysicalNetworkAndProvider(physcialNetworkId, Provider.JuniperSRX.getName()); } @@ -484,7 +491,7 @@ public class JuniperSRXExternalFirewallElement extends ExternalFirewallDeviceMan ExternalFirewallDeviceVO fwDeviceVo = _fwDevicesDao.findById(fwDeviceId); if (fwDeviceVo == null || !fwDeviceVo.getDeviceName().equalsIgnoreCase(NetworkDevice.JuniperSRXFirewall.getName())) { - throw new InvalidParameterValueException("Could not find SRX firewall device with ID " + fwDeviceId); + throw new InvalidParameterValueException("Could not find SRX firewall device by ID", null); } List networkFirewallMaps = _networkFirewallDao.listByFirewallDeviceId(fwDeviceId); diff --git a/server/src/com/cloud/network/element/NetscalerElement.java b/server/src/com/cloud/network/element/NetscalerElement.java index 437aa0557d2..eb4f39530bf 100644 --- a/server/src/com/cloud/network/element/NetscalerElement.java +++ b/server/src/com/cloud/network/element/NetscalerElement.java @@ -282,7 +282,7 @@ StaticNatServiceProvider { String deviceName = cmd.getDeviceType(); if (!isNetscalerDevice(deviceName)) { - throw new InvalidParameterValueException("Invalid Netscaler device type"); + throw new InvalidParameterValueException("Invalid Netscaler device type", null); } URI uri; @@ -291,7 +291,7 @@ StaticNatServiceProvider { } catch (Exception e) { String msg = "Error parsing the url parameter specified in addNetscalerLoadBalancer command due to " + e.getMessage(); s_logger.debug(msg); - throw new InvalidParameterValueException(msg); + throw new InvalidParameterValueException(msg, null); } Map configParams = new HashMap(); UrlUtil.parseQueryParameters(uri.getQuery(), false, configParams); @@ -300,7 +300,7 @@ StaticNatServiceProvider { if (dedicatedUse && !deviceName.equals(NetworkDevice.NetscalerVPXLoadBalancer.getName())) { String msg = "Only Netscaler VPX load balancers can be specified for dedicated use"; s_logger.debug(msg); - throw new InvalidParameterValueException(msg); + throw new InvalidParameterValueException(msg, null); } ExternalLoadBalancerDeviceVO lbDeviceVO = addExternalLoadBalancer(cmd.getPhysicalNetworkId(), cmd.getUrl(), cmd.getUsername(), cmd.getPassword(), deviceName, new NetscalerResource()); @@ -313,7 +313,7 @@ StaticNatServiceProvider { ExternalLoadBalancerDeviceVO lbDeviceVo = _lbDeviceDao.findById(lbDeviceId); if ((lbDeviceVo == null) || !isNetscalerDevice(lbDeviceVo.getDeviceName())) { - throw new InvalidParameterValueException("No netscaler device found with ID: " + lbDeviceId); + throw new InvalidParameterValueException("No netscaler device found by ID", null); } return deleteExternalLoadBalancer(lbDeviceVo.getHostId()); @@ -339,7 +339,7 @@ StaticNatServiceProvider { Map lbDetails = _detailsDao.findDetails(lbDeviceVo.getHostId()); if ((lbDeviceVo == null) || !isNetscalerDevice(lbDeviceVo.getDeviceName())) { - throw new InvalidParameterValueException("No netscaler device found with ID: " + lbDeviceId); + throw new InvalidParameterValueException("No netscaler device found by ID", null); } String deviceName = lbDeviceVo.getDeviceName(); @@ -347,7 +347,7 @@ StaticNatServiceProvider { if (NetworkDevice.NetscalerSDXLoadBalancer.getName().equalsIgnoreCase(deviceName) || NetworkDevice.NetscalerMPXLoadBalancer.getName().equalsIgnoreCase(deviceName)) { if (dedicatedUse != null && dedicatedUse == true) { - throw new InvalidParameterValueException("Netscaler MPX and SDX device should be shared and can not be dedicated to a single account."); + throw new InvalidParameterValueException("Netscaler MPX and SDX device should be shared and can not be dedicated to a single account.", null); } } @@ -417,7 +417,7 @@ StaticNatServiceProvider { ExternalLoadBalancerDeviceVO lbDeviceVo = _lbDeviceDao.findById(lbDeviceId); if (lbDeviceVo == null || !isNetscalerDevice(lbDeviceVo.getDeviceName())) { - throw new InvalidParameterValueException("Could not find Netscaler load balancer device with ID " + lbDeviceId); + throw new InvalidParameterValueException("Could not find Netscaler load balancer device by ID", null); } List networkLbMaps = _networkLBDao.listByLoadBalancerDeviceId(lbDeviceId); @@ -439,13 +439,13 @@ StaticNatServiceProvider { List lbDevices = new ArrayList(); if (physcialNetworkId == null && lbDeviceId == null) { - throw new InvalidParameterValueException("Either physical network Id or load balancer device Id must be specified"); + throw new InvalidParameterValueException("Either physical network Id or load balancer device Id must be specified", null); } if (lbDeviceId != null) { ExternalLoadBalancerDeviceVO lbDeviceVo = _lbDeviceDao.findById(lbDeviceId); if (lbDeviceVo == null || !isNetscalerDevice(lbDeviceVo.getDeviceName())) { - throw new InvalidParameterValueException("Could not find Netscaler load balancer device with ID: " + lbDeviceId); + throw new InvalidParameterValueException("Could not find Netscaler load balancer device by ID", null); } lbDevices.add(lbDeviceVo); return lbDevices; @@ -454,7 +454,7 @@ StaticNatServiceProvider { if (physcialNetworkId != null) { pNetwork = _physicalNetworkDao.findById(physcialNetworkId); if (pNetwork == null) { - throw new InvalidParameterValueException("Could not find phyical network with ID: " + physcialNetworkId); + throw new InvalidParameterValueException("Could not find phyical network by ID", null); } lbDevices = _lbDeviceDao.listByPhysicalNetworkAndProvider(physcialNetworkId, Provider.Netscaler.getName()); return lbDevices; diff --git a/server/src/com/cloud/network/lb/ElasticLoadBalancerManagerImpl.java b/server/src/com/cloud/network/lb/ElasticLoadBalancerManagerImpl.java index 06a44f15707..8b786a2f519 100644 --- a/server/src/com/cloud/network/lb/ElasticLoadBalancerManagerImpl.java +++ b/server/src/com/cloud/network/lb/ElasticLoadBalancerManagerImpl.java @@ -134,10 +134,10 @@ import com.cloud.vm.dao.NicDao; @Local(value = { ElasticLoadBalancerManager.class }) public class ElasticLoadBalancerManagerImpl implements - ElasticLoadBalancerManager, Manager, VirtualMachineGuru { +ElasticLoadBalancerManager, Manager, VirtualMachineGuru { private static final Logger s_logger = Logger .getLogger(ElasticLoadBalancerManagerImpl.class); - + @Inject IPAddressDao _ipAddressDao; @Inject @@ -196,7 +196,7 @@ public class ElasticLoadBalancerManagerImpl implements String _instance; static final private String _elbVmNamePrefix = "l"; static final private String _systemVmType = "elbvm"; - + boolean _enabled; TrafficType _frontendTrafficType = TrafficType.Guest; @@ -205,13 +205,13 @@ public class ElasticLoadBalancerManagerImpl implements ScheduledExecutorService _gcThreadPool; String _mgmtCidr; String _mgmtHost; - + Set _gcCandidateElbVmIds = Collections.newSetFromMap(new ConcurrentHashMap()); - + int _elasticLbVmRamSize; int _elasticLbvmCpuMHz; int _elasticLbvmNumCpu; - + private Long getPodIdForDirectIp(IPAddressVO ipAddr) { PodVlanMapVO podVlanMaps = _podVlanMapDao.listPodVlanMapsByVlan(ipAddr.getVlanId()); if (podVlanMaps == null) { @@ -237,19 +237,19 @@ public class ElasticLoadBalancerManagerImpl implements try { DomainRouterVO elbVm = deployELBVm(network, dest, owner, params); if (elbVm == null) { - throw new InvalidParameterValueException("Could not deploy or find existing ELB VM"); + throw new InvalidParameterValueException("Could not deploy or find existing ELB VM", null); } s_logger.debug("Deployed ELB vm = " + elbVm); return elbVm; - + } catch (Throwable t) { s_logger.warn("Error while deploying ELB VM: ", t); return null; } } - + private boolean sendCommandsToRouter(final DomainRouterVO elbVm, Commands cmds) throws AgentUnavailableException { Answer[] answers = null; @@ -310,7 +310,7 @@ public class ElasticLoadBalancerManagerImpl implements cmd.lbStatsUri = _configDao.getValue(Config.NetworkLBHaproxyStatsUri.key()); cmd.lbStatsAuth = _configDao.getValue(Config.NetworkLBHaproxyStatsAuth.key()); cmd.lbStatsPort = _configDao.getValue(Config.NetworkLBHaproxyStatsPort.key()); - + cmds.addCommand(cmd); } @@ -322,7 +322,7 @@ public class ElasticLoadBalancerManagerImpl implements // Send commands to elbVm return sendCommandsToRouter(elbVm, cmds); } - + protected DomainRouterVO findElbVmForLb(FirewallRule lb) {//TODO: use a table to lookup ElasticLbVmMapVO map = _elbVmMapDao.findOneByIp(lb.getSourceIpAddressId()); if (map == null) { @@ -332,9 +332,10 @@ public class ElasticLoadBalancerManagerImpl implements return elbVm; } + @Override public boolean applyLoadBalancerRules(Network network, List rules) - throws ResourceUnavailableException { + throws ResourceUnavailableException { if (rules == null || rules.isEmpty()) { return true; } @@ -342,9 +343,9 @@ public class ElasticLoadBalancerManagerImpl implements s_logger.warn("ELB: Not handling non-LB firewall rules"); return false; } - + DomainRouterVO elbVm = findElbVmForLb(rules.get(0)); - + if (elbVm == null) { s_logger.warn("Unable to apply lb rules, ELB vm doesn't exist in the network " + network.getId()); @@ -391,7 +392,7 @@ public class ElasticLoadBalancerManagerImpl implements } _mgmtCidr = _configDao.getValue(Config.ManagementNetwork.key()); _mgmtHost = _configDao.getValue(Config.ManagementHostIPAdr.key()); - + boolean useLocalStorage = Boolean.parseBoolean(configs.get(Config.SystemVMUseLocalStorage.key())); _elasticLbVmRamSize = NumbersUtil.parseInt(configs.get(Config.ElasticLoadBalancerVmMemory.key()), DEFAULT_ELB_VM_RAMSIZE); @@ -402,9 +403,9 @@ public class ElasticLoadBalancerManagerImpl implements true, null, true, VirtualMachine.Type.ElasticLoadBalancerVm, true); _elasticLbVmOffering.setUniqueName(ServiceOffering.elbVmDefaultOffUniqueName); _elasticLbVmOffering = _serviceOfferingDao.persistSystemServiceOffering(_elasticLbVmOffering); - - - + + + String enabled = _configDao.getValue(Config.ElasticLoadBalancerEnabled.key()); _enabled = (enabled == null) ? false: Boolean.parseBoolean(enabled); s_logger.info("Elastic Load balancer enabled: " + _enabled); @@ -425,7 +426,7 @@ public class ElasticLoadBalancerManagerImpl implements _gcThreadPool.scheduleAtFixedRate(new CleanupThread(), gcIntervalMinutes, gcIntervalMinutes, TimeUnit.MINUTES); _itMgr.registerGuru(VirtualMachine.Type.ElasticLoadBalancerVm, this); } - + return true; } @@ -457,9 +458,9 @@ public class ElasticLoadBalancerManagerImpl implements } return null; } - + public DomainRouterVO deployELBVm(Network guestNetwork, DeployDestination dest, Account owner, Map params) throws - ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException { + ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException { long dcId = dest.getDataCenter().getId(); // lock guest network @@ -480,13 +481,13 @@ public class ElasticLoadBalancerManagerImpl implements s_logger.debug("Starting a ELB vm for network configurations: " + 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; + || guestNetwork.getState() == Network.State.Setup + || guestNetwork.getState() == Network.State.Implementing + : "Network is not yet fully implemented: "+ guestNetwork; DataCenterDeployment plan = null; DomainRouterVO elbVm = null; - + plan = new DataCenterDeployment(dcId, dest.getPod().getId(), null, null, null, null); if (elbVm == null) { @@ -494,7 +495,7 @@ public class ElasticLoadBalancerManagerImpl implements if (s_logger.isDebugEnabled()) { s_logger.debug("Creating the ELB vm " + id); } - + List offerings = _networkMgr.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemControlNetwork); NetworkOfferingVO controlOffering = offerings.get(0); NetworkVO controlConfig = _networkMgr.setupNetwork(_systemAcct, controlOffering, plan, null, null, false).get(0); @@ -505,7 +506,7 @@ public class ElasticLoadBalancerManagerImpl implements networks.add(new Pair(controlConfig, null)); networks.add(new Pair((NetworkVO) guestNetwork, guestNic)); - + VMTemplateVO template = _templateDao.findSystemVMTemplate(dcId); String typeString = "ElasticLoadBalancerVm"; @@ -518,7 +519,7 @@ public class ElasticLoadBalancerManagerImpl implements if (vrProvider == null) { throw new CloudRuntimeException("Cannot find virtual router provider " + typeString + " as service provider " + provider.getId()); } - + elbVm = new DomainRouterVO(id, _elasticLbVmOffering.getId(), vrProvider.getId(), VirtualMachineName.getSystemVmName(id, _instance, _elbVmNamePrefix), template.getId(), template.getHypervisorType(), template.getGuestOSId(), owner.getDomainId(), owner.getId(), false, 0, false, RedundantState.UNKNOWN, @@ -539,7 +540,7 @@ public class ElasticLoadBalancerManagerImpl implements _networkDao.releaseFromLockTable(guestNetworkId); } } - + private DomainRouterVO start(DomainRouterVO elbVm, User user, Account caller, Map params) throws StorageUnavailableException, InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException { s_logger.debug("Starting ELB VM " + elbVm); @@ -549,8 +550,8 @@ public class ElasticLoadBalancerManagerImpl implements return null; } } - - + + private DomainRouterVO stop(DomainRouterVO elbVm, boolean forced, User user, Account caller) throws ConcurrentOperationException, ResourceUnavailableException { s_logger.debug("Stopping ELB vm " + elbVm); try { @@ -563,7 +564,7 @@ public class ElasticLoadBalancerManagerImpl implements throw new CloudRuntimeException("Unable to stop " + elbVm, e); } } - + protected List findExistingLoadBalancers(String lbName, Long ipId, Long accountId, Long domainId, Integer publicPort) { SearchBuilder sb = _lbDao.createSearchBuilder(); sb.and("name", sb.entity().getName(), SearchCriteria.Op.EQ); @@ -591,16 +592,16 @@ public class ElasticLoadBalancerManagerImpl implements sc.setParameters("publicPort", publicPort); } List lbs = _lbDao.search(sc, null); - + return lbs == null || lbs.size()==0 ? null: lbs; } - + @DB public PublicIp allocDirectIp(Account account, long guestNetworkId) throws InsufficientAddressCapacityException { Network frontEndNetwork = _networkMgr.getNetwork(guestNetworkId); Transaction txn = Transaction.currentTxn(); txn.start(); - + PublicIp ip = _networkMgr.assignPublicIpAddress(frontEndNetwork.getDataCenterId(), null, account, VlanType.DirectAttached, frontEndNetwork.getId(), null, true); IPAddressVO ipvo = _ipAddressDao.findById(ip.getId()); ipvo.setAssociatedWithNetworkId(frontEndNetwork.getId()); @@ -610,25 +611,25 @@ public class ElasticLoadBalancerManagerImpl implements return ip; } - + public void releaseIp(long ipId, long userId, Account caller) { s_logger.info("ELB: Release public IP for loadbalancing " + ipId); IPAddressVO ipvo = _ipAddressDao.findById(ipId); ipvo.setAssociatedWithNetworkId(null); _ipAddressDao.update(ipvo.getId(), ipvo); - _networkMgr.disassociatePublicIpAddress(ipId, userId, caller); - _ipAddressDao.unassignIpAddress(ipId); + _networkMgr.disassociatePublicIpAddress(ipId, userId, caller); + _ipAddressDao.unassignIpAddress(ipId); } @Override @DB public LoadBalancer handleCreateLoadBalancerRule(CreateLoadBalancerRuleCmd lb, Account account, long networkId) throws InsufficientAddressCapacityException, NetworkRuleConflictException { //this part of code is executed when the LB provider is Elastic Load Balancer vm - if (!_networkMgr.isProviderSupportServiceInNetwork(lb.getNetworkId(), Service.Lb, Provider.ElasticLoadBalancerVm)) { - return null; - } - - Long ipId = lb.getSourceIpAddressId(); + if (!_networkMgr.isProviderSupportServiceInNetwork(lb.getNetworkId(), Service.Lb, Provider.ElasticLoadBalancerVm)) { + return null; + } + + Long ipId = lb.getSourceIpAddressId(); if (ipId != null) { return null; } @@ -646,7 +647,7 @@ public class ElasticLoadBalancerManagerImpl implements if (lb.getSourceIpAddressId() != null) { existingLbs = findExistingLoadBalancers(lb.getName(), null, lb.getAccountId(), lb.getDomainId(), null); if (existingLbs != null) { - throw new InvalidParameterValueException("Supplied LB name " + lb.getName() + " is not associated with IP " + lb.getSourceIpAddressId() ); + throw new InvalidParameterValueException("Supplied LB name " + lb.getName() + " is not associated with IP " + lb.getSourceIpAddressId(), null); } } else { s_logger.debug("Could not find any existing frontend ips for this account for this LB rule, acquiring a new frontent IP for ELB"); @@ -665,7 +666,7 @@ public class ElasticLoadBalancerManagerImpl implements Network network = _networkMgr.getNetwork(networkId); IPAddressVO ipAddr = _ipAddressDao.findById(ipId); - + LoadBalancer result = null; try { lb.setSourceIpAddressId(ipId); @@ -698,26 +699,26 @@ public class ElasticLoadBalancerManagerImpl implements elbVm = _routerDao.findById(elbVmMap.getElbVmId()); } } - + if (elbVm == null) { s_logger.warn("No ELB VM can be found or deployed"); s_logger.warn("Deleting LB since we failed to deploy ELB VM"); _lbDao.remove(result.getId()); return null; } - + ElasticLbVmMapVO mapping = new ElasticLbVmMapVO(ipId, elbVm.getId(), result.getId()); _elbVmMapDao.persist(mapping); return result; - + } finally { if (account != null) { _accountDao.releaseFromLockTable(account.getId()); } } - + } - + void garbageCollectUnusedElbVms() { List unusedElbVms = _elbVmMapDao.listUnusedElbVms(); if (unusedElbVms != null && unusedElbVms.size() > 0) @@ -759,12 +760,12 @@ public class ElasticLoadBalancerManagerImpl implements } _gcCandidateElbVmIds = currentGcCandidates; } - + public class CleanupThread implements Runnable { @Override public void run() { garbageCollectUnusedElbVms(); - + } CleanupThread() { @@ -784,7 +785,7 @@ public class ElasticLoadBalancerManagerImpl implements } } - + @Override public DomainRouterVO findByName(String name) { if (!VirtualMachineName.isValidSystemVmName(name, _instance, _elbVmNamePrefix)) { @@ -810,7 +811,7 @@ public class ElasticLoadBalancerManagerImpl implements @Override public boolean finalizeVirtualMachineProfile(VirtualMachineProfile profile, DeployDestination dest, ReservationContext context) { DomainRouterVO elbVm = profile.getVirtualMachine(); - + List elbNics = profile.getNics(); Long guestNtwkId = null; for (NicProfile routerNic : elbNics) { @@ -819,7 +820,7 @@ public class ElasticLoadBalancerManagerImpl implements break; } } - + NetworkVO guestNetwork = _networkDao.findById(guestNtwkId); DataCenter dc = dest.getDataCenter(); @@ -876,7 +877,7 @@ public class ElasticLoadBalancerManagerImpl implements if (defaultDns2 != null) { buf.append(" dns2=").append(defaultDns2); } - + if (s_logger.isDebugEnabled()) { s_logger.debug("Boot Args for " + profile + ": " + buf.toString()); } @@ -930,7 +931,7 @@ public class ElasticLoadBalancerManagerImpl implements NicProfile controlNic = null; Long guestNetworkId = null; - + if(profile.getHypervisorType() == HypervisorType.VMware && dcVo.getNetworkType() == NetworkType.Basic) { // TODO this is a ugly to test hypervisor type here // for basic network mode, we will use the guest NIC for control NIC @@ -984,7 +985,7 @@ public class ElasticLoadBalancerManagerImpl implements processStopOrRebootAnswer(elbVm, answer); } } - + public void processStopOrRebootAnswer(final DomainRouterVO elbVm, Answer answer) { //TODO: process network usage stats } @@ -993,7 +994,7 @@ public class ElasticLoadBalancerManagerImpl implements @Override public void finalizeExpunge(DomainRouterVO vm) { // no-op - + } @Override @@ -1004,7 +1005,7 @@ public class ElasticLoadBalancerManagerImpl implements return VirtualMachineName.getSystemVmId(vmName); } - + @Override public boolean plugNic(Network network, NicTO nic, VirtualMachineTO vm, ReservationContext context, DeployDestination dest) throws ConcurrentOperationException, ResourceUnavailableException, @@ -1022,17 +1023,17 @@ public class ElasticLoadBalancerManagerImpl implements } - @Override - public void prepareStop(VirtualMachineProfile profile) { - - } + @Override + public void prepareStop(VirtualMachineProfile profile) { + + } - @Override - public boolean recreateNeeded( - VirtualMachineProfile profile, long hostId, - Commands cmds, ReservationContext context) { - // TODO Auto-generated method stub - return false; - } + @Override + public boolean recreateNeeded( + VirtualMachineProfile profile, long hostId, + Commands cmds, ReservationContext context) { + // TODO Auto-generated method stub + return false; + } } diff --git a/server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java b/server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java index a1c3c6f39a2..ebe627f3490 100755 --- a/server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java +++ b/server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java @@ -223,7 +223,7 @@ public class LoadBalancingRulesManagerImpl implements LoadBalancingRulesMa serviceResponse.setName(service.getName()); if ("Lb".equalsIgnoreCase(service.getName())) { Map serviceCapabilities = serviceCapabilitiesMap - .get(service); + .get(service); if (serviceCapabilities != null) { for (Capability capability : serviceCapabilities .keySet()) { @@ -260,15 +260,15 @@ public class LoadBalancingRulesManagerImpl implements LoadBalancingRulesMa String csUrl = _configDao.getValue(Config.EndpointeUrl.key()); if(apiKey == null) { - throw new InvalidParameterValueException("apiKey for user: " + user.getUsername() + " is empty. Please generate it"); + throw new InvalidParameterValueException("apiKey for user: " + user.getUsername() + " is empty. Please generate it", null); } if(secretKey == null) { - throw new InvalidParameterValueException("secretKey for user: " + user.getUsername() + " is empty. Please generate it"); + throw new InvalidParameterValueException("secretKey for user: " + user.getUsername() + " is empty. Please generate it", null); } if(csUrl == null || csUrl.contains("localhost")) { - throw new InvalidParameterValueException("Global setting endpointe.url has to be set to the Management Server's API end point"); + throw new InvalidParameterValueException("Global setting endpointe.url has to be set to the Management Server's API end point", null); } LbAutoScaleVmProfile lbAutoScaleVmProfile = new LbAutoScaleVmProfile(autoScaleVmProfile, apiKey, secretKey, csUrl); @@ -922,7 +922,7 @@ public class LoadBalancingRulesManagerImpl implements LoadBalancingRulesMa try { if (ipVO.getAssociatedWithNetworkId() == null) { boolean assignToVpcNtwk = network.getVpcId() != null - && ipVO.getVpcId() != null && ipVO.getVpcId().longValue() == network.getVpcId(); + && ipVO.getVpcId() != null && ipVO.getVpcId().longValue() == network.getVpcId(); if (assignToVpcNtwk) { // set networkId just for verification purposes _networkMgr.checkIpForService(ipVO, Service.Lb, lb.getNetworkId()); @@ -936,7 +936,7 @@ public class LoadBalancingRulesManagerImpl implements LoadBalancingRulesMa } if (ipVO.getAssociatedWithNetworkId() == null) { - throw new InvalidParameterValueException("Ip address " + ipVO + " is not assigned to the network " + network); + throw new InvalidParameterValueException("Ip address " + ipVO + " is not assigned to the network " + network, null); } if (lb.getSourceIpAddressId() == null) { diff --git a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java index 2a240ab5d92..86b843c17b2 100755 --- a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java +++ b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java @@ -130,10 +130,8 @@ import com.cloud.network.PhysicalNetworkServiceProvider; import com.cloud.network.PublicIpAddress; import com.cloud.network.RemoteAccessVpn; import com.cloud.network.Site2SiteCustomerGateway; -import com.cloud.network.Site2SiteCustomerGatewayVO; import com.cloud.network.Site2SiteVpnConnection; import com.cloud.network.Site2SiteVpnConnectionVO; -import com.cloud.network.Site2SiteVpnGatewayVO; import com.cloud.network.SshKeysDistriMonitor; import com.cloud.network.VirtualNetworkApplianceService; import com.cloud.network.VirtualRouterProvider; @@ -231,7 +229,7 @@ import com.cloud.vm.dao.UserVmDetailsDao; */ @Local(value = { VirtualNetworkApplianceManager.class, VirtualNetworkApplianceService.class }) public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplianceManager, VirtualNetworkApplianceService, - VirtualMachineGuru, Listener { +VirtualMachineGuru, Listener { private static final Logger s_logger = Logger.getLogger(VirtualNetworkApplianceManagerImpl.class); String _name; @@ -323,7 +321,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian Site2SiteVpnConnectionDao _s2sVpnConnectionDao; @Inject Site2SiteVpnManager _s2sVpnMgr; - + int _routerRamSize; int _routerCpuMHz; int _retry = 2; @@ -342,7 +340,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian private String _usageTimeZone = "GMT"; private final long mgmtSrvrId = MacAddress.getMacAddress().toLong(); private static final int ACQUIRE_GLOBAL_LOCK_TIMEOUT_FOR_COOPERATION = 5; // 5 seconds - + ScheduledExecutorService _executor; ScheduledExecutorService _checkExecutor; ScheduledExecutorService _networkStatsUpdateExecutor; @@ -394,7 +392,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian DomainRouterVO router = _routerDao.findById(routerId); if (router == null) { - throw new InvalidParameterValueException("Unable to find router with id " + routerId); + throw new InvalidParameterValueException("Unable to find router by id", null); } _accountMgr.checkAccess(caller, null, true, router); @@ -406,19 +404,19 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian ServiceOffering newServiceOffering = _configMgr.getServiceOffering(serviceOfferingId); if (newServiceOffering == null) { - throw new InvalidParameterValueException("Unable to find service offering with id " + serviceOfferingId); + throw new InvalidParameterValueException("Unable to find service offering by id", null); } // check if it is a system service offering, if yes return with error as it cannot be used for user vms if (!newServiceOffering.getSystemUse()) { - throw new InvalidParameterValueException("Cannot upgrade router vm to a non system service offering " + serviceOfferingId); + throw new InvalidParameterValueException("Cannot upgrade router vm to a non system service offering " + serviceOfferingId, null); } // 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."); + + "; make sure the router is stopped and not in an error state before upgrading.", null); } ServiceOfferingVO currentServiceOffering = _serviceOfferingDao.findById(router.getServiceOfferingId()); @@ -427,8 +425,8 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian // offering 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()); + newServiceOffering.getUseLocalStorage() + " is different from " + + "curruent local storage status: " + currentServiceOffering.getUseLocalStorage(), null); } router.setServiceOfferingId(serviceOfferingId); @@ -482,7 +480,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian // verify parameters DomainRouterVO router = _routerDao.findById(routerId); if (router == null) { - throw new InvalidParameterValueException("Unable to find router by id " + routerId + "."); + throw new InvalidParameterValueException("Unable to find router by id", null); } _accountMgr.checkAccess(account, null, true, router); @@ -506,21 +504,21 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian for (Long guestNtwkId : routerGuestNtwkIds) { final UserStatisticsVO userStats = _userStatsDao.lock(router.getAccountId(), router.getDataCenterIdToDeployIn(), guestNtwkId, null, router.getId(), router.getType().toString()); - if (userStats != null) { - final long currentBytesRcvd = userStats.getCurrentBytesReceived(); - userStats.setCurrentBytesReceived(0); - userStats.setNetBytesReceived(userStats.getNetBytesReceived() + currentBytesRcvd); + if (userStats != null) { + final long currentBytesRcvd = userStats.getCurrentBytesReceived(); + userStats.setCurrentBytesReceived(0); + userStats.setNetBytesReceived(userStats.getNetBytesReceived() + currentBytesRcvd); - final long currentBytesSent = userStats.getCurrentBytesSent(); - userStats.setCurrentBytesSent(0); - userStats.setNetBytesSent(userStats.getNetBytesSent() + currentBytesSent); - _userStatsDao.update(userStats.getId(), userStats); - s_logger.debug("Successfully updated user statistics as a part of domR " + router + " reboot/stop"); - } else { - s_logger.warn("User stats were not created for account " + router.getAccountId() + " and dc " + router.getDataCenterIdToDeployIn()); + final long currentBytesSent = userStats.getCurrentBytesSent(); + userStats.setCurrentBytesSent(0); + userStats.setNetBytesSent(userStats.getNetBytesSent() + currentBytesSent); + _userStatsDao.update(userStats.getId(), userStats); + s_logger.debug("Successfully updated user statistics as a part of domR " + router + " reboot/stop"); + } else { + s_logger.warn("User stats were not created for account " + router.getAccountId() + " and dc " + router.getDataCenterIdToDeployIn()); + } } - } - + txn.commit(); } catch (final Exception e) { txn.rollback(); @@ -536,7 +534,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian // verify parameters DomainRouterVO router = _routerDao.findById(routerId); if (router == null) { - throw new InvalidParameterValueException("Unable to find domain router with id " + routerId + "."); + throw new InvalidParameterValueException("Unable to find domain router by id", null); } _accountMgr.checkAccess(caller, null, true, router); @@ -584,7 +582,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian value = configs.get("router.check.interval"); _routerCheckInterval = NumbersUtil.parseInt(value, 30); - + _instance = configs.get("instance.name"); if (_instance == null) { _instance = "DEFAULT"; @@ -657,29 +655,29 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian int HOURLY_TIME = 60; final int DAILY_TIME = 60 * 24; if (_usageAggregationRange == DAILY_TIME) { - cal.roll(Calendar.DAY_OF_YEAR, false); - cal.set(Calendar.HOUR_OF_DAY, 0); - cal.set(Calendar.MINUTE, 0); - cal.set(Calendar.SECOND, 0); - cal.set(Calendar.MILLISECOND, 0); - cal.roll(Calendar.DAY_OF_YEAR, true); - cal.add(Calendar.MILLISECOND, -1); - endDate = cal.getTime().getTime(); + cal.roll(Calendar.DAY_OF_YEAR, false); + cal.set(Calendar.HOUR_OF_DAY, 0); + cal.set(Calendar.MINUTE, 0); + cal.set(Calendar.SECOND, 0); + cal.set(Calendar.MILLISECOND, 0); + cal.roll(Calendar.DAY_OF_YEAR, true); + cal.add(Calendar.MILLISECOND, -1); + endDate = cal.getTime().getTime(); } else if (_usageAggregationRange == HOURLY_TIME) { - cal.roll(Calendar.HOUR_OF_DAY, false); - cal.set(Calendar.MINUTE, 0); - cal.set(Calendar.SECOND, 0); - cal.set(Calendar.MILLISECOND, 0); - cal.roll(Calendar.HOUR_OF_DAY, true); - cal.add(Calendar.MILLISECOND, -1); - endDate = cal.getTime().getTime(); + cal.roll(Calendar.HOUR_OF_DAY, false); + cal.set(Calendar.MINUTE, 0); + cal.set(Calendar.SECOND, 0); + cal.set(Calendar.MILLISECOND, 0); + cal.roll(Calendar.HOUR_OF_DAY, true); + cal.add(Calendar.MILLISECOND, -1); + endDate = cal.getTime().getTime(); } else { - endDate = cal.getTime().getTime(); + endDate = cal.getTime().getTime(); } _networkStatsUpdateExecutor.scheduleAtFixedRate(new NetworkStatsUpdateTask(), (endDate - System.currentTimeMillis()), (_usageAggregationRange * 60 * 1000), TimeUnit.MILLISECONDS); - + if (_routerCheckInterval > 0) { _checkExecutor.scheduleAtFixedRate(new CheckRouterTask(), _routerCheckInterval, _routerCheckInterval, TimeUnit.SECONDS); } else { @@ -728,11 +726,11 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian cmd.addVmData("metadata", "public-hostname", StringUtils.unicodeEscape(vmName)); } else { - if (router.getPublicIpAddress() == null) { - cmd.addVmData("metadata", "public-ipv4", guestIpAddress); - } else { - cmd.addVmData("metadata", "public-ipv4", router.getPublicIpAddress()); - } + if (router.getPublicIpAddress() == null) { + cmd.addVmData("metadata", "public-ipv4", guestIpAddress); + } else { + cmd.addVmData("metadata", "public-ipv4", router.getPublicIpAddress()); + } cmd.addVmData("metadata", "public-hostname", router.getPublicIpAddress()); } cmd.addVmData("metadata", "instance-id", vmInstanceName); @@ -766,7 +764,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian if (privateIP != null) { List routerGuestNtwkIds = _routerDao.getRouterNetworks(router.getId()); - + for (Long guestNtwkId : routerGuestNtwkIds) { boolean forVpc = router.getVpcId() != null; Network guestNtwk = _networkMgr.getNetwork(guestNtwkId); @@ -777,8 +775,8 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian _networkMgr.getNetworkTag(router.getHypervisorType(), guestNtwk)); final NetworkUsageCommand usageCmd = new NetworkUsageCommand(privateIP, router.getHostName(), forVpc, _itMgr.toNicTO(guestNicProfile, router.getHypervisorType())); - UserStatisticsVO previousStats = _statsDao.findBy(router.getAccountId(), - router.getDataCenterIdToDeployIn(), guestNtwkId, null, router.getId(), router.getType().toString()); + UserStatisticsVO previousStats = _statsDao.findBy(router.getAccountId(), + router.getDataCenterIdToDeployIn(), guestNtwkId, null, router.getId(), router.getType().toString()); NetworkUsageAnswer answer = null; try { answer = (NetworkUsageAnswer) _agentMgr.easySend(router.getHostId(), usageCmd); @@ -786,7 +784,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian s_logger.warn("Error while collecting network stats from router: "+router.getInstanceName()+" from host: "+router.getHostId(), e); continue; } - + if (answer != null) { if (!answer.getResult()) { s_logger.warn("Error while collecting network stats from router: "+router.getInstanceName()+" from host: "+router.getHostId() + "; details: " + answer.getDetails()); @@ -799,27 +797,27 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian continue; } txn.start(); - UserStatisticsVO stats = _statsDao.lock(router.getAccountId(), - router.getDataCenterIdToDeployIn(), guestNtwkId, null, router.getId(), router.getType().toString()); + UserStatisticsVO stats = _statsDao.lock(router.getAccountId(), + router.getDataCenterIdToDeployIn(), guestNtwkId, null, router.getId(), router.getType().toString()); if (stats == null) { s_logger.warn("unable to find stats for account: " + router.getAccountId()); continue; } - + if(previousStats != null - && ((previousStats.getCurrentBytesReceived() != stats.getCurrentBytesReceived()) - || (previousStats.getCurrentBytesSent() != stats.getCurrentBytesSent()))){ - s_logger.debug("Router stats changed from the time NetworkUsageCommand was sent. " + - "Ignoring current answer. Router: "+answer.getRouterName()+" Rcvd: " + - answer.getBytesReceived()+ "Sent: " +answer.getBytesSent()); + && ((previousStats.getCurrentBytesReceived() != stats.getCurrentBytesReceived()) + || (previousStats.getCurrentBytesSent() != stats.getCurrentBytesSent()))){ + s_logger.debug("Router stats changed from the time NetworkUsageCommand was sent. " + + "Ignoring current answer. Router: "+answer.getRouterName()+" Rcvd: " + + answer.getBytesReceived()+ "Sent: " +answer.getBytesSent()); continue; } - + 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. Router: " + - answer.getRouterName()+" Reported: " + answer.getBytesReceived() + s_logger.debug("Received # of bytes that's less than the last one. " + + "Assuming something went wrong and persisting it. Router: " + + answer.getRouterName()+" Reported: " + answer.getBytesReceived() + " Stored: " + stats.getCurrentBytesReceived()); } stats.setNetBytesReceived(stats.getNetBytesReceived() + stats.getCurrentBytesReceived()); @@ -827,9 +825,9 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian 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. Router: " + - answer.getRouterName()+" Reported: " + answer.getBytesSent() + s_logger.debug("Received # of bytes that's less than the last one. " + + "Assuming something went wrong and persisting it. Router: " + + answer.getRouterName()+" Reported: " + answer.getBytesSent() + " Stored: " + stats.getCurrentBytesSent()); } stats.setNetBytesSent(stats.getNetBytesSent() + stats.getCurrentBytesSent()); @@ -839,15 +837,15 @@ 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(); } } + } } } - } } catch (Exception e) { s_logger.warn("Error while collecting network stats", e); } @@ -861,37 +859,37 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian @Override public void run() { - GlobalLock scanLock = GlobalLock.getInternLock("network.stats"); + GlobalLock scanLock = GlobalLock.getInternLock("network.stats"); try { if(scanLock.lock(ACQUIRE_GLOBAL_LOCK_TIMEOUT_FOR_COOPERATION)) { - //Check for ownership - //msHost in UP state with min id should run the job - ManagementServerHostVO msHost = _msHostDao.findOneInUpState(new Filter(ManagementServerHostVO.class, "id", true, 0L, 1L)); - if(msHost == null || (msHost.getMsid() != mgmtSrvrId)){ - s_logger.debug("Skipping aggregate network stats update"); - scanLock.unlock(); - return; - } - Transaction txn = Transaction.open(Transaction.CLOUD_DB); + //Check for ownership + //msHost in UP state with min id should run the job + ManagementServerHostVO msHost = _msHostDao.findOneInUpState(new Filter(ManagementServerHostVO.class, "id", true, 0L, 1L)); + if(msHost == null || (msHost.getMsid() != mgmtSrvrId)){ + s_logger.debug("Skipping aggregate network stats update"); + scanLock.unlock(); + return; + } + Transaction txn = Transaction.open(Transaction.CLOUD_DB); try { - txn.start(); - //get all stats with delta > 0 - List updatedStats = _statsDao.listUpdatedStats(); - Date updatedTime = new Date(); - for(UserStatisticsVO stat : updatedStats){ - //update agg bytes - stat.setAggBytesReceived(stat.getCurrentBytesReceived() + stat.getNetBytesReceived()); - stat.setAggBytesSent(stat.getCurrentBytesSent() + stat.getNetBytesSent()); - _userStatsDao.update(stat.getId(), stat); - //insert into op_user_stats_log - UserStatsLogVO statsLog = new UserStatsLogVO(stat.getId(), stat.getNetBytesReceived(), stat.getNetBytesSent(), stat.getCurrentBytesReceived(), - stat.getCurrentBytesSent(), stat.getAggBytesReceived(), stat.getAggBytesSent(), updatedTime); - _userStatsLogDao.persist(statsLog); - } - s_logger.debug("Successfully updated aggregate network stats"); - txn.commit(); + txn.start(); + //get all stats with delta > 0 + List updatedStats = _statsDao.listUpdatedStats(); + Date updatedTime = new Date(); + for(UserStatisticsVO stat : updatedStats){ + //update agg bytes + stat.setAggBytesReceived(stat.getCurrentBytesReceived() + stat.getNetBytesReceived()); + stat.setAggBytesSent(stat.getCurrentBytesSent() + stat.getNetBytesSent()); + _userStatsDao.update(stat.getId(), stat); + //insert into op_user_stats_log + UserStatsLogVO statsLog = new UserStatsLogVO(stat.getId(), stat.getNetBytesReceived(), stat.getNetBytesSent(), stat.getCurrentBytesReceived(), + stat.getCurrentBytesSent(), stat.getAggBytesReceived(), stat.getAggBytesSent(), updatedTime); + _userStatsLogDao.persist(statsLog); + } + s_logger.debug("Successfully updated aggregate network stats"); + txn.commit(); } catch (Exception e){ - txn.rollback(); + txn.rollback(); s_logger.debug("Failed to update aggregate network stats", e); } finally { scanLock.unlock(); @@ -978,7 +976,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } } } - + protected void updateRoutersRedundantState(List routers) { boolean updated = false; for (DomainRouterVO router : routers) { @@ -1102,61 +1100,61 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian if (!router.getIsRedundantRouter()) { continue; } - + List routerGuestNtwkIds = _routerDao.getRouterNetworks(router.getId()); - + for (Long routerGuestNtwkId : routerGuestNtwkIds) { if (checkedNetwork.contains(routerGuestNtwkId)) { - continue; - } + continue; + } checkedNetwork.add(routerGuestNtwkId); List checkingRouters = _routerDao.listByNetworkAndRole(routerGuestNtwkId, Role.VIRTUAL_ROUTER); - if (checkingRouters.size() != 2) { - continue; - } - DomainRouterVO masterRouter = null; - DomainRouterVO backupRouter = null; - for (DomainRouterVO r : checkingRouters) { - if (r.getRedundantState() == RedundantState.MASTER) { - if (masterRouter == null) { - masterRouter = r; - } else { - //Duplicate master! We give up, until the admin fix duplicate MASTER issue - break; - } - } else if (r.getRedundantState() == RedundantState.BACKUP) { - if (backupRouter == null) { - backupRouter = r; - } else { - break; + if (checkingRouters.size() != 2) { + continue; + } + DomainRouterVO masterRouter = null; + DomainRouterVO backupRouter = null; + for (DomainRouterVO r : checkingRouters) { + if (r.getRedundantState() == RedundantState.MASTER) { + if (masterRouter == null) { + masterRouter = r; + } else { + //Duplicate master! We give up, until the admin fix duplicate MASTER issue + break; + } + } else if (r.getRedundantState() == RedundantState.BACKUP) { + if (backupRouter == null) { + backupRouter = r; + } else { + break; + } } } - } - if (masterRouter != null && backupRouter != null) { - if (getRealPriority(masterRouter) - DEFAULT_DELTA + 1 != getRealPriority(backupRouter) || backupRouter.getIsPriorityBumpUp()) { - recoverRedundantNetwork(masterRouter, backupRouter); + if (masterRouter != null && backupRouter != null) { + if (getRealPriority(masterRouter) - DEFAULT_DELTA + 1 != getRealPriority(backupRouter) || backupRouter.getIsPriorityBumpUp()) { + recoverRedundantNetwork(masterRouter, backupRouter); + } } } } } - } private void checkDuplicateMaster(List routers) { Map networkRouterMaps = new HashMap(); for (DomainRouterVO router : routers) { List routerGuestNtwkIds = _routerDao.getRouterNetworks(router.getId()); - + for (Long routerGuestNtwkId : routerGuestNtwkIds) { - if (router.getRedundantState() == RedundantState.MASTER) { + if (router.getRedundantState() == RedundantState.MASTER) { if (networkRouterMaps.containsKey(routerGuestNtwkId)) { DomainRouterVO dupRouter = networkRouterMaps.get(routerGuestNtwkId); - String title = "More than one redundant virtual router is in MASTER state! Router " + router.getHostName() + " and router " + dupRouter.getHostName(); - String context = "Virtual router (name: " + router.getHostName() + ", id: " + router.getId() + " and router (name: " - + dupRouter.getHostName() + ", id: " + router.getId() + ") are both in MASTER state! If the problem persist, restart both of routers. "; + String title = "More than one redundant virtual router is in MASTER state! Router " + router.getHostName() + " and router " + dupRouter.getHostName(); + String context = "Virtual router (name: " + router.getHostName() + ", id: " + router.getId() + " and router (name: " + + dupRouter.getHostName() + ", id: " + router.getId() + ") are both in MASTER state! If the problem persist, restart both of routers. "; - _alertMgr.sendAlert(AlertManager.ALERT_TYPE_DOMAIN_ROUTER, router.getDataCenterIdToDeployIn(), router.getPodIdToDeployIn(), title, context); - _alertMgr.sendAlert(AlertManager.ALERT_TYPE_DOMAIN_ROUTER, dupRouter.getDataCenterIdToDeployIn(), dupRouter.getPodIdToDeployIn(), title, context); - } else { + _alertMgr.sendAlert(AlertManager.ALERT_TYPE_DOMAIN_ROUTER, router.getDataCenterIdToDeployIn(), router.getPodIdToDeployIn(), title, context); + _alertMgr.sendAlert(AlertManager.ALERT_TYPE_DOMAIN_ROUTER, dupRouter.getDataCenterIdToDeployIn(), dupRouter.getPodIdToDeployIn(), title, context); + } else { networkRouterMaps.put(routerGuestNtwkId, router); } } @@ -1237,7 +1235,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian for (HostVO h : hosts) { if (h.getStatus() == Status.Up) { s_logger.debug("Pick up host that has hypervisor type " + h.getHypervisorType() + " in cluster " + - cv.getId() + " to start domain router for OVM"); + cv.getId() + " to start domain router for OVM"); return h.getHypervisorType(); } } @@ -1258,10 +1256,10 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian assert guestNetwork.getState() == Network.State.Implemented || guestNetwork.getState() == Network.State.Setup || guestNetwork.getState() == Network.State.Implementing : "Network is not yet fully implemented: " - + guestNetwork; + + guestNetwork; assert guestNetwork.getTrafficType() == TrafficType.Guest; - + //1) Get deployment plan and find out the list of routers boolean isPodBased = (dest.getDataCenter().getNetworkType() == NetworkType.Basic || @@ -1271,7 +1269,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian Pair> planAndRouters = getDeploymentPlanAndRouters(isPodBased, dest, guestNetwork.getId()); DeploymentPlan plan = planAndRouters.first(); List routers = planAndRouters.second(); - + //2) Figure out required routers count int routerCount = 1; if (isRedundant) { @@ -1344,9 +1342,9 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian new Pair(publicNetwork, sourceNatIp)); DomainRouterVO router = deployRouter(owner, dest, plan, params, isRedundant, vrProvider, offeringId, null, networks); - + _routerDao.addRouterToGuestNetwork(router, network); - + routers.add(router); } } finally { @@ -1362,7 +1360,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian Long vpcId, List> networks) throws ConcurrentOperationException, InsufficientAddressCapacityException, InsufficientServerCapacityException, InsufficientCapacityException, StorageUnavailableException, ResourceUnavailableException { - + long id = _routerDao.getNextInSequence(Long.class, "id"); if (s_logger.isDebugEnabled()) { s_logger.debug("Creating the router " + id + " in datacenter " + dest.getDataCenter()); @@ -1375,29 +1373,29 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian List supportedHypervisors = new ArrayList(); HypervisorType defaults = _resourceMgr.getDefaultHypervisor(dest.getDataCenter().getId()); if (defaults != HypervisorType.None) { - supportedHypervisors.add(defaults); + supportedHypervisors.add(defaults); } - + if (dest.getCluster() != null) { if (dest.getCluster().getHypervisorType() == HypervisorType.Ovm) { - supportedHypervisors.add(getClusterToStartDomainRouterForOvm(dest.getCluster().getPodId())); + supportedHypervisors.add(getClusterToStartDomainRouterForOvm(dest.getCluster().getPodId())); } else { - supportedHypervisors.add(dest.getCluster().getHypervisorType()); + supportedHypervisors.add(dest.getCluster().getHypervisorType()); } } else { supportedHypervisors = _resourceMgr.getSupportedHypervisorTypes(dest.getDataCenter().getId(), true, - plan.getPodId()); + plan.getPodId()); } - + if (supportedHypervisors.isEmpty()) { - if (plan.getPodId() != null) { - throw new InsufficientServerCapacityException("Unable to create virtual router, " + - "there are no clusters in the pod ", Pod.class, plan.getPodId()); - } - throw new InsufficientServerCapacityException("Unable to create virtual router, " + - "there are no clusters in the zone ", DataCenter.class, dest.getDataCenter().getId()); + if (plan.getPodId() != null) { + throw new InsufficientServerCapacityException("Unable to create virtual router, " + + "there are no clusters in the pod ", Pod.class, plan.getPodId()); + } + throw new InsufficientServerCapacityException("Unable to create virtual router, " + + "there are no clusters in the zone ", DataCenter.class, dest.getDataCenter().getId()); } - + int allocateRetry = 0; int startRetry = 0; DomainRouterVO router = null; @@ -1411,7 +1409,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian s_logger.debug(hType + " won't support system vm, skip it"); continue; } - + boolean offerHA = routerOffering.getOfferHA(); /* We don't provide HA to redundant router VMs, admin should own it all, and redundant router themselves are HA */ if (isRedundant) { @@ -1419,9 +1417,9 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } router = new DomainRouterVO(id, routerOffering.getId(), vrProvider.getId(), - VirtualMachineName.getRouterName(id, _instance), template.getId(), template.getHypervisorType(), - template.getGuestOSId(), owner.getDomainId(), owner.getId(), isRedundant, 0, false, - RedundantState.UNKNOWN, offerHA, false, vpcId); + VirtualMachineName.getRouterName(id, _instance), template.getId(), template.getHypervisorType(), + template.getGuestOSId(), owner.getDomainId(), owner.getId(), isRedundant, 0, false, + RedundantState.UNKNOWN, offerHA, false, vpcId); router.setRole(Role.VIRTUAL_ROUTER); router = _itMgr.allocate(router, template, routerOffering, networks, plan, null, owner); } catch (InsufficientCapacityException ex) { @@ -1434,14 +1432,14 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } finally { allocateRetry++; } - + try { router = startVirtualRouter(router, _accountMgr.getSystemUser(), _accountMgr.getSystemAccount(), params); break; } catch (InsufficientCapacityException ex) { if (startRetry < 2 && iter.hasNext()) { - s_logger.debug("Failed to start the domR " + router + " with hypervisor type " + hType + ", " + - "destroying it and recreating one more time"); + s_logger.debug("Failed to start the domR " + router + " with hypervisor type " + hType + ", " + + "destroying it and recreating one more time"); //destroy the router destroyRouter(router.getId()); continue; @@ -1452,7 +1450,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian startRetry++; } } - + return router; } @@ -1460,15 +1458,15 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian DeploymentPlan plan, Network guestNetwork, Pair publicNetwork) throws ConcurrentOperationException, InsufficientAddressCapacityException { - + boolean setupPublicNetwork = false; if (publicNetwork != null) { setupPublicNetwork = publicNetwork.first(); } - + //Form networks List> networks = new ArrayList>(3); - + //1) Guest network boolean hasGuestNetwork = false; if (guestNetwork != null) { @@ -1500,7 +1498,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } else { gatewayNic.setDefaultNic(true); } - + networks.add(new Pair((NetworkVO) guestNetwork, gatewayNic)); hasGuestNetwork = true; } @@ -1511,8 +1509,8 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian NetworkOfferingVO controlOffering = offerings.get(0); NetworkVO controlConfig = _networkMgr.setupNetwork(_systemAcct, controlOffering, plan, null, null, false).get(0); networks.add(new Pair(controlConfig, null)); - - + + //3) Public network if (setupPublicNetwork) { PublicIp sourceNatIp = publicNetwork.second(); @@ -1538,7 +1536,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian return networks; } - + protected Pair> getDeploymentPlanAndRouters(boolean isPodBased, DeployDestination dest, long guestNetworkId) { long dcId = dest.getDataCenter().getId(); @@ -1557,16 +1555,16 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } else { routers = _routerDao.listByNetworkAndRole(guestNetworkId, Role.VIRTUAL_ROUTER); } - + return new Pair>(plan, routers); } - - + + private DomainRouterVO startVirtualRouter(DomainRouterVO router, User user, Account caller, Map params) throws StorageUnavailableException, InsufficientCapacityException, - ConcurrentOperationException, ResourceUnavailableException { - - if (router.getRole() != Role.VIRTUAL_ROUTER || !router.getIsRedundantRouter()) { + ConcurrentOperationException, ResourceUnavailableException { + + if (router.getRole() != Role.VIRTUAL_ROUTER || !router.getIsRedundantRouter()) { return this.start(router, user, caller, params, null); } @@ -1632,16 +1630,16 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian @Override public List deployVirtualRouterInGuestNetwork(Network guestNetwork, DeployDestination dest, Account owner, Map params, boolean isRedundant) throws InsufficientCapacityException, - ConcurrentOperationException, ResourceUnavailableException { + ConcurrentOperationException, ResourceUnavailableException { List routers = findOrDeployVirtualRouterInGuestNetwork (guestNetwork, dest, owner, isRedundant, params); - + return startRouters(params, routers); } protected List startRouters(Map params, List routers) throws StorageUnavailableException, InsufficientCapacityException, ConcurrentOperationException, - ResourceUnavailableException { + ResourceUnavailableException { List runningRouters = null; if (routers != null) { @@ -1672,7 +1670,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian @Override public boolean finalizeVirtualMachineProfile(VirtualMachineProfile profile, DeployDestination dest, ReservationContext context) { - + boolean dnsProvided = true; boolean dhcpProvided = true; boolean publicNetwork = false; @@ -1691,9 +1689,9 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian buf.append(" name=").append(profile.getHostName()); if (Boolean.valueOf(_configDao.getValue("system.vm.random.password"))) { - buf.append(" vmpassword=").append(_configDao.getValue("system.vm.password")); + buf.append(" vmpassword=").append(_configDao.getValue("system.vm.password")); } - + NicProfile controlNic = null; String defaultDns1 = null; String defaultDns2 = null; @@ -1701,7 +1699,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian int deviceId = nic.getDeviceId(); 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()); defaultDns1 = nic.getDns1(); @@ -1716,7 +1714,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian if (dest.getHost().getHypervisorType() == HypervisorType.VMware) { 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() + + dest.getPod().getCidrAddress() + "/" + dest.getPod().getCidrSize() + ", pod gateway: " + dest.getPod().getGateway() + ", management host: " + _mgmt_host); } @@ -1752,7 +1750,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian if (controlNic == null) { throw new CloudRuntimeException("Didn't start a control port"); } - + String rpValue = _configDao.getValue(Config.NetworkRouterRpFilter.key()); if (rpValue != null && rpValue.equalsIgnoreCase("true")) { _disable_rp_filter = true; @@ -1766,20 +1764,20 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian type = "vpcrouter"; if (_disable_rp_filter) { rpFilter=" disable_rp_filter=true"; - } + } } else if (!publicNetwork) { type = "dhcpsrvr"; } else { type = "router"; if (_disable_rp_filter) { rpFilter=" disable_rp_filter=true"; - } + } } - + if (_disable_rp_filter) { rpFilter=" disable_rp_filter=true"; } - + buf.append(" type=" + type + rpFilter); String domain_suffix = dc.getDetail(ZoneConfig.DnsSearchOrder.getName()); @@ -1790,7 +1788,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian if (profile.getHypervisorType() == HypervisorType.VMware) { buf.append(" extra_pubnics=" + _routerExtraPublicNics); } - + /* If virtual router didn't provide DNS service but provide DHCP service, we need to override the DHCP response * to return DNS server rather than * virtual router itself. */ @@ -1819,9 +1817,9 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } return true; - } + } + - protected StringBuilder createGuestBootLoadArgs(NicProfile guestNic, String defaultDns1, String defaultDns2, DomainRouterVO router) { long guestNetworkId = guestNic.getNetworkId(); @@ -1830,7 +1828,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian DataCenterVO dc = _dcDao.findById(guestNetwork.getDataCenterId()); StringBuilder buf = new StringBuilder(); - + boolean isRedundant = router.getIsRedundantRouter(); if (isRedundant) { buf.append(" redundant_router=1"); @@ -1855,7 +1853,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian if (domain != null) { buf.append(" domain=" + domain); } - + //setup dhcp range if (dc.getNetworkType() == NetworkType.Basic) { if (guestNic.isDefaultNic()) { @@ -1871,11 +1869,11 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian dhcpRange = NetUtils.getDhcpRange(cidr); } } - + if (dhcpRange != null) { buf.append(" dhcprange=" + dhcpRange); } - + return buf; } @@ -2003,7 +2001,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian s_logger.debug("Reapplying dhcp entries as a part of domR " + router + " start..."); createDhcpEntryCommandsForVMs(router, cmds, guestNetworkId); } - + if (_networkMgr.isProviderSupportServiceInNetwork(guestNetworkId, Service.UserData, provider)) { // Resend user data s_logger.debug("Reapplying vm data (userData and metaData) entries as a part of domR " + router + " start..."); @@ -2013,7 +2011,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian protected void finalizeNetworkRulesForNetwork(Commands cmds, DomainRouterVO router, Provider provider, Long guestNetworkId) { s_logger.debug("Resending ipAssoc, port forwarding, load balancing rules as a part of Virtual router start"); - + ArrayList publicIps = getPublicIpsToApply(router, provider, guestNetworkId); if (publicIps != null && !publicIps.isEmpty()) { @@ -2022,7 +2020,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian List staticNatFirewallRules = new ArrayList(); List staticNats = new ArrayList(); List firewallRules = new ArrayList(); - + //Get information about all the rules (StaticNats and StaticNatRules; PFVPN to reapply on domR start) for (PublicIpAddress ip : publicIps) { if (_networkMgr.isProviderSupportServiceInNetwork(guestNetworkId, Service.PortForwarding, provider)) { @@ -2034,41 +2032,41 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian if (_networkMgr.isProviderSupportServiceInNetwork(guestNetworkId, Service.Firewall, provider)) { firewallRules.addAll(_rulesDao.listByIpAndPurpose(ip.getId(), Purpose.Firewall)); } - + if (_networkMgr.isProviderSupportServiceInNetwork(guestNetworkId, Service.Vpn, provider)) { RemoteAccessVpn vpn = _vpnDao.findById(ip.getId()); if (vpn != null) { vpns.add(vpn); } } - + if (_networkMgr.isProviderSupportServiceInNetwork(guestNetworkId, Service.StaticNat, provider)) { if (ip.isOneToOneNat()) { - String dstIp = _networkMgr.getIpInNetwork(ip.getAssociatedWithVmId(), guestNetworkId); - StaticNatImpl staticNat = new StaticNatImpl(ip.getAccountId(), ip.getDomainId(), guestNetworkId, ip.getId(), dstIp, false); + String dstIp = _networkMgr.getIpInNetwork(ip.getAssociatedWithVmId(), guestNetworkId); + StaticNatImpl staticNat = new StaticNatImpl(ip.getAccountId(), ip.getDomainId(), guestNetworkId, ip.getId(), dstIp, false); staticNats.add(staticNat); } } } - + //Re-apply static nats s_logger.debug("Found " + staticNats.size() + " static nat(s) to apply as a part of domR " + router + " start."); if (!staticNats.isEmpty()) { createApplyStaticNatCommands(staticNats, router, cmds, guestNetworkId); } - + //Re-apply firewall rules s_logger.debug("Found " + staticNats.size() + " firewall rule(s) to apply as a part of domR " + router + " start."); if (!firewallRules.isEmpty()) { createFirewallRulesCommands(firewallRules, router, cmds, guestNetworkId); } - + // 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, guestNetworkId); } - + // 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()) { @@ -2078,7 +2076,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } createApplyStaticNatRulesCommands(staticNatRules, router, cmds, guestNetworkId); } - + // Re-apply vpn rules s_logger.debug("Found " + vpns.size() + " vpn(s) to apply as a part of domR " + router + " start."); if (!vpns.isEmpty()) { @@ -2086,7 +2084,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian createApplyVpnCommands(vpn, router, cmds); } } - + List lbs = _loadBalancerDao.listByNetworkId(guestNetworkId); List lbRules = new ArrayList(); if (_networkMgr.isProviderSupportServiceInNetwork(guestNetworkId, Service.Lb, provider)) { @@ -2098,19 +2096,19 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian 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, guestNetworkId); + createApplyLoadBalancingRulesCommands(lbRules, router, cmds, guestNetworkId); } } } protected void finalizeIpAssocForNetwork(Commands cmds, VirtualRouter router, Provider provider, Long guestNetworkId, Map vlanMacAddress) { - + ArrayList publicIps = getPublicIpsToApply(router, provider, guestNetworkId); - + if (publicIps != null && !publicIps.isEmpty()) { s_logger.debug("Found " + publicIps.size() + " ip(s) to apply as a part of domR " + router + " start."); // Re-apply public ip addresses - should come before PF/LB/VPN @@ -2137,7 +2135,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } } } - + if (addIp) { PublicIp publicIp = new PublicIp(userIp, _vlanDao.findById(userIp.getVlanId()), NetUtils.createSequenceBasedMacAddress(userIp.getMacAddress())); @@ -2145,13 +2143,13 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } } } - + //Get public Ips that should be handled by router Network network = _networkDao.findById(guestNetworkId); Map> ipToServices = _networkMgr.getIpToServices(allPublicIps, false, false); Map> providerToIpList = _networkMgr.getProviderToIpList(network, ipToServices); // Only cover virtual router for now, if ELB use it this need to be modified - + ArrayList publicIps = providerToIpList.get(provider); return publicIps; } @@ -2160,7 +2158,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian public boolean finalizeStart(VirtualMachineProfile profile, long hostId, Commands cmds, ReservationContext context) { DomainRouterVO router = profile.getVirtualMachine(); - + boolean result = true; Answer answer = cmds.getAnswer("checkSsh"); @@ -2176,10 +2174,10 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian if (result == false) { return result; } - + //Get guest networks info List guestNetworks = new ArrayList(); - + List routerNics = _nicDao.listByVmId(profile.getId()); for (Nic routerNic : routerNics) { Network network = _networkMgr.getNetwork(routerNic.getNetworkId()); @@ -2187,7 +2185,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian guestNetworks.add(network); } } - + answer = cmds.getAnswer("getDomRVersion"); if (answer != null && answer instanceof GetDomRVersionAnswer) { GetDomRVersionAnswer versionAnswer = (GetDomRVersionAnswer)answer; @@ -2234,7 +2232,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian if (router.getState() != State.Running) { 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()); + + router.getState(), DataCenter.class, network.getDataCenterId()); } Commands cmds = new Commands(OnError.Stop); @@ -2258,7 +2256,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian answer = cmds.getAnswer("startVpn"); if (!answer.getResult()) { s_logger.error("Unable to start vpn in zone " + router.getDataCenterIdToDeployIn() + " for account " + - vpn.getAccountId() + " on domR: " + router.getInstanceName() + " due to " + vpn.getAccountId() + " on domR: " + router.getInstanceName() + " due to " + answer.getDetails()); throw new ResourceUnavailableException("Unable to start vpn in zone " + router.getDataCenterIdToDeployIn() + " for account " + vpn.getAccountId() + " on domR: " + router.getInstanceName() @@ -2302,7 +2300,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } else { s_logger.warn("Failed to delete remote access VPN: domR " + router + " 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()); + router.getState(), DataCenter.class, network.getDataCenterId()); } } @@ -2312,7 +2310,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian private DomainRouterVO start(DomainRouterVO router, User user, Account caller, Map params, DeploymentPlan planToDeploy) throws StorageUnavailableException, InsufficientCapacityException, - ConcurrentOperationException, ResourceUnavailableException { + ConcurrentOperationException, ResourceUnavailableException { s_logger.debug("Starting router " + router); if (_itMgr.start(router, params, user, caller, planToDeploy) != null) { return _routerDao.findById(router.getId()); @@ -2334,24 +2332,24 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian throw new CloudRuntimeException("Unable to stop " + router, e); } } - + @Override public boolean applyDhcpEntry(Network network, final NicProfile nic, VirtualMachineProfile profile, DeployDestination dest, List routers) - throws ResourceUnavailableException { + throws ResourceUnavailableException { _userVmDao.loadDetails((UserVmVO) profile.getVirtualMachine()); - + final VirtualMachineProfile updatedProfile = profile; final boolean isZoneBasic = (dest.getDataCenter().getNetworkType() == NetworkType.Basic); final Long podId = isZoneBasic ? dest.getPod().getId() : null; - + boolean podLevelException = false; //for user vm in Basic zone we should try to re-deploy vm in a diff pod if it fails to deploy in original pod; so throwing exception with Pod scope if (isZoneBasic && podId != null && updatedProfile.getVirtualMachine().getType() == VirtualMachine.Type.User && network.getTrafficType() == TrafficType.Guest && network.getGuestType() == Network.GuestType.Shared) { podLevelException = true; } - + return applyRules(network, routers, "dhcp entry", podLevelException, podId, true, new RuleApplier() { @Override public boolean execute(Network network, VirtualRouter router) throws ResourceUnavailableException { @@ -2367,45 +2365,45 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian }); } - private String findDefaultDnsIp(long userVmId) { - NicVO defaultNic = _nicDao.findDefaultNicForVM(userVmId); - - //check if DNS provider is the domR - if (!_networkMgr.isProviderSupportServiceInNetwork(defaultNic.getNetworkId(), Service.Dns, Provider.VirtualRouter)) { - return null; - } - - NetworkOfferingVO offering = _networkOfferingDao.findById(_networkDao.findById(defaultNic.getNetworkId()).getNetworkOfferingId()); - if (offering.getRedundantRouter()) { - return findGatewayIp(userVmId); - } - - //find domR's nic in the network - NicVO domrDefaultNic = _nicDao.findByNetworkIdAndType(defaultNic.getNetworkId(), VirtualMachine.Type.DomainRouter); - return domrDefaultNic.getIp4Address(); - } - - private String findGatewayIp(long userVmId) { - NicVO defaultNic = _nicDao.findDefaultNicForVM(userVmId); - return defaultNic.getGateway(); - } + private String findDefaultDnsIp(long userVmId) { + NicVO defaultNic = _nicDao.findDefaultNicForVM(userVmId); + + //check if DNS provider is the domR + if (!_networkMgr.isProviderSupportServiceInNetwork(defaultNic.getNetworkId(), Service.Dns, Provider.VirtualRouter)) { + return null; + } + + NetworkOfferingVO offering = _networkOfferingDao.findById(_networkDao.findById(defaultNic.getNetworkId()).getNetworkOfferingId()); + if (offering.getRedundantRouter()) { + return findGatewayIp(userVmId); + } + + //find domR's nic in the network + NicVO domrDefaultNic = _nicDao.findByNetworkIdAndType(defaultNic.getNetworkId(), VirtualMachine.Type.DomainRouter); + return domrDefaultNic.getIp4Address(); + } + + private String findGatewayIp(long userVmId) { + NicVO defaultNic = _nicDao.findDefaultNicForVM(userVmId); + return defaultNic.getGateway(); + } @Override public boolean applyUserData(Network network, final NicProfile nic, VirtualMachineProfile profile, DeployDestination dest, List routers) throws ResourceUnavailableException { _userVmDao.loadDetails((UserVmVO) profile.getVirtualMachine()); - + final VirtualMachineProfile updatedProfile = profile; final boolean isZoneBasic = (dest.getDataCenter().getNetworkType() == NetworkType.Basic); final Long podId = isZoneBasic ? dest.getPod().getId() : null; - + boolean podLevelException = false; //for user vm in Basic zone we should try to re-deploy vm in a diff pod if it fails to deploy in original pod; so throwing exception with Pod scope if (isZoneBasic && podId != null && updatedProfile.getVirtualMachine().getType() == VirtualMachine.Type.User && network.getTrafficType() == TrafficType.Guest && network.getGuestType() == Network.GuestType.Shared) { podLevelException = true; } - + return applyRules(network, routers, "userdata and password entry", podLevelException, podId, false, new RuleApplier() { @Override public boolean execute(Network network, VirtualRouter router) throws ResourceUnavailableException { @@ -2424,7 +2422,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian @Override public DomainRouterVO persist(DomainRouterVO router) { - DomainRouterVO virtualRouter = _routerDao.persist(router); + DomainRouterVO virtualRouter = _routerDao.persist(router); return virtualRouter; } @@ -2434,7 +2432,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian if (routers == null || routers.isEmpty()) { 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()); + network.getId(), DataCenter.class, network.getDataCenterId()); } boolean agentResults = true; @@ -2443,7 +2441,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian if (router.getState() != State.Running) { 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()); + router.getState(), DataCenter.class, network.getDataCenterId()); } Commands cmds = new Commands(OnError.Continue); @@ -2515,7 +2513,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian // verify parameters DomainRouterVO router = _routerDao.findById(routerId); if (router == null) { - throw new InvalidParameterValueException("Unable to find router by id " + routerId + "."); + throw new InvalidParameterValueException("Unable to find router by id", null); } _accountMgr.checkAccess(caller, null, true, router); @@ -2566,7 +2564,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } //domR doesn't support release for sourceNat IP address; so reset the state if (ipAddress.isSourceNat() && ipAddress.getState() == IpAddress.State.Releasing) { - ipAddress.setState(IpAddress.State.Allocated); + ipAddress.setState(IpAddress.State.Allocated); } ipList.add(ipAddress); vlanIpMap.put(vlanTag, ipList); @@ -2641,13 +2639,13 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } SetPortForwardingRulesCommand cmd = null; - + if (router.getVpcId() != null) { cmd = new SetPortForwardingRulesVpcCommand(rulesTO); } else { cmd = new SetPortForwardingRulesCommand(rulesTO); } - + cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, getRouterControlIp(router.getId())); cmd.setAccessDetail(NetworkElementCommand.ROUTER_GUEST_IP, getRouterIpInNetwork(guestNetworkId, router.getId())); cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName()); @@ -2699,7 +2697,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian DomainRouterVO domr = _routerDao.findById(router.getId()); routerPublicIp = domr.getPublicIpAddress(); } - + Network guestNetwork = _networkMgr.getNetwork(guestNetworkId); Nic nic = _nicDao.findByInstanceIdAndNetworkId(guestNetwork.getId(), router.getId()); NicProfile nicProfile = new NicProfile(nic, guestNetwork, nic.getBroadcastUri(), nic.getIsolationUri(), @@ -2756,7 +2754,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian cmds.addCommand("users", addUsersCmd); cmds.addCommand("startVpn", startVpnCmd); } - + private void createPasswordCommand(VirtualRouter router, VirtualMachineProfile profile, NicVO nic, Commands cmds) { String password = (String) profile.getParameter(VirtualMachineProfile.Param.VmPassword); DataCenterVO dcVo = _dcDao.findById(router.getDataCenterIdToDeployIn()); @@ -2772,16 +2770,16 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian cmds.addCommand("password", cmd); } - + } - + private void createVmDataCommand(VirtualRouter router, UserVm vm, NicVO nic, String publicKey, Commands cmds) { String serviceOffering = _serviceOfferingDao.findByIdIncludingRemoved(vm.getServiceOfferingId()).getDisplayText(); String zoneName = _dcDao.findById(router.getDataCenterIdToDeployIn()).getName(); cmds.addCommand("vmdata", generateVmDataCommand(router, nic.getIp4Address(), vm.getUserData(), serviceOffering, zoneName, nic.getIp4Address(), vm.getHostName(), vm.getInstanceName(), vm.getId(), publicKey, nic.getNetworkId())); - + } private void createVmDataCommandForVMs(DomainRouterVO router, Commands cmds, long guestNetworkId) { @@ -2802,7 +2800,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } } } - + private void createDhcpEntryCommand(VirtualRouter router, UserVm vm, NicVO nic, Commands cmds) { DhcpEntryCommand dhcpCommand = new DhcpEntryCommand(nic.getMacAddress(), nic.getIp4Address(), vm.getHostName()); DataCenterVO dcVo = _dcDao.findById(router.getDataCenterIdToDeployIn()); @@ -3036,11 +3034,11 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian public String getDnsBasicZoneUpdate() { return _dnsBasicZoneUpdates; } - + protected interface RuleApplier { boolean execute(Network network, VirtualRouter router) throws ResourceUnavailableException; } - + protected boolean applyRules(Network network, List routers, String typeString, boolean isPodLevelException, Long podId, boolean failWhenDisconnect, RuleApplier applier) throws ResourceUnavailableException { if (routers == null || routers.isEmpty()) { @@ -3050,10 +3048,10 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian DataCenter dc = _dcDao.findById(network.getDataCenterId()); boolean isZoneBasic = (dc.getNetworkType() == NetworkType.Basic); - + // isPodLevelException and podId is only used for basic zone assert !((!isZoneBasic && isPodLevelException) || (isZoneBasic && isPodLevelException && podId == null)); - + List connectedRouters = new ArrayList(); List disconnectedRouters = new ArrayList(); boolean result = true; @@ -3065,11 +3063,11 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian if (router.isStopPending()) { if (_hostDao.findById(router.getHostId()).getStatus() == Status.Up) { throw new ResourceUnavailableException("Unable to process due to the stop pending router " + - router.getInstanceName() + " haven't been stopped after it's host coming back!", + router.getInstanceName() + " haven't been stopped after it's host coming back!", DataCenter.class, router.getDataCenterIdToDeployIn()); } s_logger.debug("Router " + router.getInstanceName() + " is stop pending, so not sending apply " + - typeString + " commands to the backend"); + typeString + " commands to the backend"); continue; } try { @@ -3221,7 +3219,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } @Override - + public boolean processDisconnect(long agentId, Status state) { return false; } @@ -3240,17 +3238,17 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian routerControlIpAddress = n.getIp4Address(); } } - + if(routerControlIpAddress == null) { s_logger.warn("Unable to find router's control ip in its attached NICs!. routerId: " + routerId); DomainRouterVO router = _routerDao.findById(routerId); return router.getPrivateIpAddress(); } - + return routerControlIpAddress; } - - + + protected String getRouterIpInNetwork(long networkId, long instanceId) { return _nicDao.getIpAddress(networkId, instanceId); } @@ -3267,23 +3265,23 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian @Override public boolean unplugNic(Network network, NicTO nic, VirtualMachineTO vm, ReservationContext context, DeployDestination dest) throws ConcurrentOperationException, ResourceUnavailableException { - //not supported + //not supported throw new UnsupportedOperationException("Unplug nic is not supported for vm of type " + vm.getType()); } - + @Override public void prepareStop(VirtualMachineProfile profile){ - //Collect network usage before stopping Vm - VMInstanceVO vm = profile.getVirtualMachine(); + //Collect network usage before stopping Vm + VMInstanceVO vm = profile.getVirtualMachine(); DomainRouterVO router = _routerDao.findById(vm.getId()); if(router == null){ - return; + return; } String privateIP = router.getPrivateIpAddress(); if (privateIP != null) { List routerGuestNtwkIds = _routerDao.getRouterNetworks(router.getId()); - + for (Long guestNtwkId : routerGuestNtwkIds) { boolean forVpc = router.getVpcId() != null; Network guestNtwk = _networkMgr.getNetwork(guestNtwkId); @@ -3294,8 +3292,8 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian _networkMgr.getNetworkTag(router.getHypervisorType(), guestNtwk)); final NetworkUsageCommand usageCmd = new NetworkUsageCommand(privateIP, router.getHostName(), forVpc, _itMgr.toNicTO(guestNicProfile, router.getHypervisorType())); - UserStatisticsVO previousStats = _statsDao.findBy(router.getAccountId(), - router.getDataCenterIdToDeployIn(), guestNtwkId, null, router.getId(), router.getType().toString()); + UserStatisticsVO previousStats = _statsDao.findBy(router.getAccountId(), + router.getDataCenterIdToDeployIn(), guestNtwkId, null, router.getId(), router.getType().toString()); NetworkUsageAnswer answer = null; try { answer = (NetworkUsageAnswer) _agentMgr.easySend(router.getHostId(), usageCmd); @@ -3303,7 +3301,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian s_logger.warn("Error while collecting network stats from router: "+router.getInstanceName()+" from host: "+router.getHostId(), e); continue; } - + if (answer != null) { if (!answer.getResult()) { s_logger.warn("Error while collecting network stats from router: "+router.getInstanceName()+" from host: "+router.getHostId() + "; details: " + answer.getDetails()); @@ -3316,27 +3314,27 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian continue; } txn.start(); - UserStatisticsVO stats = _statsDao.lock(router.getAccountId(), - router.getDataCenterIdToDeployIn(), guestNtwkId, null, router.getId(), router.getType().toString()); + UserStatisticsVO stats = _statsDao.lock(router.getAccountId(), + router.getDataCenterIdToDeployIn(), guestNtwkId, null, router.getId(), router.getType().toString()); if (stats == null) { s_logger.warn("unable to find stats for account: " + router.getAccountId()); continue; } if(previousStats != null - && ((previousStats.getCurrentBytesReceived() != stats.getCurrentBytesReceived()) - || (previousStats.getCurrentBytesSent() != stats.getCurrentBytesSent()))){ - s_logger.debug("Router stats changed from the time NetworkUsageCommand was sent. " + - "Ignoring current answer. Router: "+answer.getRouterName()+" Rcvd: " + - answer.getBytesReceived()+ "Sent: " +answer.getBytesSent()); + && ((previousStats.getCurrentBytesReceived() != stats.getCurrentBytesReceived()) + || (previousStats.getCurrentBytesSent() != stats.getCurrentBytesSent()))){ + s_logger.debug("Router stats changed from the time NetworkUsageCommand was sent. " + + "Ignoring current answer. Router: "+answer.getRouterName()+" Rcvd: " + + answer.getBytesReceived()+ "Sent: " +answer.getBytesSent()); continue; } 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. Router: " + - answer.getRouterName()+" Reported: " + answer.getBytesReceived() + s_logger.debug("Received # of bytes that's less than the last one. " + + "Assuming something went wrong and persisting it. Router: " + + answer.getRouterName()+" Reported: " + answer.getBytesReceived() + " Stored: " + stats.getCurrentBytesReceived()); } stats.setNetBytesReceived(stats.getNetBytesReceived() + stats.getCurrentBytesReceived()); @@ -3344,9 +3342,9 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian 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. Router: " + - answer.getRouterName()+" Reported: " + answer.getBytesSent() + s_logger.debug("Received # of bytes that's less than the last one. " + + "Assuming something went wrong and persisting it. Router: " + + answer.getRouterName()+" Reported: " + answer.getBytesSent() + " Stored: " + stats.getCurrentBytesSent()); } stats.setNetBytesSent(stats.getNetBytesSent() + stats.getCurrentBytesSent()); @@ -3356,8 +3354,8 @@ 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(); } @@ -3366,16 +3364,16 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } } - @Override - public boolean recreateNeeded( - VirtualMachineProfile profile, long hostId, - Commands cmds, ReservationContext context) { - //asssume that if failed to ssh into router, meaning router is crashed - CheckSshAnswer answer = (CheckSshAnswer) cmds.getAnswer("checkSsh"); - if (answer == null || !answer.getResult()) { - return true; - } + @Override + public boolean recreateNeeded( + VirtualMachineProfile profile, long hostId, + Commands cmds, ReservationContext context) { + //asssume that if failed to ssh into router, meaning router is crashed + CheckSshAnswer answer = (CheckSshAnswer) cmds.getAnswer("checkSsh"); + if (answer == null || !answer.getResult()) { + return true; + } - return false; - } + return false; + } } diff --git a/server/src/com/cloud/network/rules/RulesManagerImpl.java b/server/src/com/cloud/network/rules/RulesManagerImpl.java index e2b1d33c3a7..658f4931745 100755 --- a/server/src/com/cloud/network/rules/RulesManagerImpl.java +++ b/server/src/com/cloud/network/rules/RulesManagerImpl.java @@ -209,9 +209,9 @@ public class RulesManagerImpl implements RulesManager, RulesService, Manager { } else { _networkMgr.checkIpForService(ipAddress, Service.PortForwarding, null); } - + if (ipAddress.getAssociatedWithNetworkId() == null) { - throw new InvalidParameterValueException("Ip address " + ipAddress + " is not assigned to the network " + network); + throw new InvalidParameterValueException("Ip address " + ipAddress + " is not assigned to the network " + network, null); } try { @@ -295,9 +295,9 @@ public class RulesManagerImpl implements RulesManager, RulesService, Manager { //if the rule is the last one for the ip address assigned to VPC, unassign it from the network IpAddress ip = _ipAddressDao.findById(ipAddress.getId()); _networkMgr.unassignIPFromVpcNetwork(ip.getId(), networkId); - } } } + } @Override @DB @@ -418,7 +418,7 @@ public class RulesManagerImpl implements RulesManager, RulesService, Manager { if (network == null) { throw new InvalidParameterValueException("Unable to find network by id", null); } - + // Check that vm has a nic in the network Nic guestNic = _networkMgr.getNicInNetwork(vmId, networkId); if (guestNic == null) { @@ -433,7 +433,7 @@ public class RulesManagerImpl implements RulesManager, RulesService, Manager { throw new InvalidParameterValueException("Unable to create static nat rule; StaticNat service is not " + "supported in network with specified id", idList); } - + if (!isSystemVm) { //associate ip address to network (if needed) if (ipAddress.getAssociatedWithNetworkId() == null) { @@ -441,7 +441,7 @@ public class RulesManagerImpl implements RulesManager, RulesService, Manager { && ipAddress.getVpcId() != null && ipAddress.getVpcId().longValue() == network.getVpcId(); if (assignToVpcNtwk) { _networkMgr.checkIpForService(ipAddress, Service.StaticNat, networkId); - + s_logger.debug("The ip is not associated with the VPC network id="+ networkId + ", so assigning"); try { ipAddress = _networkMgr.associateIPToGuestNetwork(ipId, networkId, false); @@ -455,18 +455,18 @@ public class RulesManagerImpl implements RulesManager, RulesService, Manager { } else { _networkMgr.checkIpForService(ipAddress, Service.StaticNat, null); } - + if (ipAddress.getAssociatedWithNetworkId() == null) { - throw new InvalidParameterValueException("Ip address " + ipAddress + " is not assigned to the network " + network); + throw new InvalidParameterValueException("Ip address " + ipAddress + " is not assigned to the network " + network, null); } // Check permissions checkIpAndUserVm(ipAddress, vm, caller); - + // Verify ip address parameter isIpReadyForStaticNat(vmId, ipAddress, caller, ctx.getCallerUserId()); } - + ipAddress.setOneToOneNat(true); ipAddress.setAssociatedWithVmId(vmId); @@ -480,21 +480,21 @@ public class RulesManagerImpl implements RulesManager, RulesService, Manager { } } else { s_logger.warn("Failed to update ip address " + ipAddress + " in the DB as a part of enableStaticNat"); - + } } finally { if (!result) { ipAddress.setOneToOneNat(false); ipAddress.setAssociatedWithVmId(null); _ipAddressDao.update(ipAddress.getId(), ipAddress); - + if (performedIpAssoc) { //if the rule is the last one for the ip address assigned to VPC, unassign it from the network IpAddress ip = _ipAddressDao.findById(ipAddress.getId()); _networkMgr.unassignIPFromVpcNetwork(ip.getId(), networkId); - } - } - } + } + } + } return result; } @@ -1395,9 +1395,9 @@ public class RulesManagerImpl implements RulesManager, RulesService, Manager { } protected void removePFRule(PortForwardingRuleVO rule) { - + _portForwardingDao.remove(rule.getId()); - + //if the rule is the last one for the ip address assigned to VPC, unassign it from the network IpAddress ip = _ipAddressDao.findById(rule.getSourceIpAddressId()); _networkMgr.unassignIPFromVpcNetwork(ip.getId(), rule.getNetworkId()); diff --git a/server/src/com/cloud/network/vpc/VpcManagerImpl.java b/server/src/com/cloud/network/vpc/VpcManagerImpl.java index 263c47d65f6..9efa8028878 100644 --- a/server/src/com/cloud/network/vpc/VpcManagerImpl.java +++ b/server/src/com/cloud/network/vpc/VpcManagerImpl.java @@ -191,7 +191,7 @@ public class VpcManagerImpl implements VpcManager, Manager{ Map configs = configDao.getConfiguration(params); String value = configs.get(Config.VpcCleanupInterval.key()); _cleanupInterval = NumbersUtil.parseInt(value, 60 * 60); // 1 hour - + String maxNtwks = configs.get(Config.VpcMaxNetworks.key()); _maxNetworks = NumbersUtil.parseInt(maxNtwks, 3); // max=3 is default return true; @@ -978,8 +978,8 @@ public class VpcManagerImpl implements VpcManager, Manager{ throw new CloudRuntimeException("Number of networks per VPC can't extend " + _maxNetworks + "; increase it using global config " + Config.VpcMaxNetworks); } - - + + //1) CIDR is required if (cidr == null) { throw new InvalidParameterValueException("Gateway/netmask are required when create network for VPC", null); @@ -1021,17 +1021,17 @@ public class VpcManagerImpl implements VpcManager, Manager{ throw new InvalidParameterValueException("Network domain of the new network should match network" + " domain of vpc with specified vpcId", idList); } - + //6) gateway should never be equal to the cidr subnet if (NetUtils.getCidrSubNet(cidr).equalsIgnoreCase(gateway)) { - throw new InvalidParameterValueException("Invalid gateway specified. It should never be equal to the cidr subnet value"); + throw new InvalidParameterValueException("Invalid gateway specified. It should never be equal to the cidr subnet value", null); } //7) gateway should never be equal to the cidr broadcast ip if (NetUtils.getCidrBroadcastIp(cidr).equalsIgnoreCase(gateway)) { - throw new InvalidParameterValueException("Invalid gateway specified. It should never be equal to the cidr broadcast ip"); + throw new InvalidParameterValueException("Invalid gateway specified. It should never be equal to the cidr broadcast ip", null); } - + } finally { s_logger.debug("Releasing lock for " + locked); _vpcDao.releaseFromLockTable(locked.getId()); @@ -1062,7 +1062,7 @@ public class VpcManagerImpl implements VpcManager, Manager{ _s2sVpnMgr.cleanupVpnConnectionByVpc(vpcId); s_logger.debug("Cleaning up existed site to site VPN gateways"); _s2sVpnMgr.cleanupVpnGatewayByVpc(vpcId); - + //2) release all ip addresses List ipsToRelease = _ipAddressDao.listByAssociatedVpc(vpcId, null); s_logger.debug("Releasing ips for vpc id=" + vpcId + " as a part of vpc cleanup"); @@ -1680,9 +1680,10 @@ public class VpcManagerImpl implements VpcManager, Manager{ public VpcGateway getPrivateGatewayForVpc(long vpcId) { return _vpcGatewayDao.getPrivateGatewayForVpc(vpcId); } - + + @Override public int getMaxNetworksPerVpc() { return _maxNetworks; } - + } diff --git a/server/src/com/cloud/server/ManagementServerExtImpl.java b/server/src/com/cloud/server/ManagementServerExtImpl.java index dbfd78559fd..a6cb48a4a82 100644 --- a/server/src/com/cloud/server/ManagementServerExtImpl.java +++ b/server/src/com/cloud/server/ManagementServerExtImpl.java @@ -35,6 +35,7 @@ import com.cloud.user.Account; import com.cloud.user.AccountVO; import com.cloud.user.UserContext; import com.cloud.user.dao.AccountDao; +import com.cloud.utils.IdentityProxy; import com.cloud.utils.component.ComponentLocator; import com.cloud.utils.db.Filter; import com.cloud.utils.db.SearchCriteria; @@ -96,21 +97,21 @@ public class ManagementServerExtImpl extends ManagementServerImpl implements Man Long domainId = cmd.getDomainId(); String accountName = cmd.getAccountName(); Account userAccount = null; - Account caller = (Account)UserContext.current().getCaller(); + Account caller = UserContext.current().getCaller(); Long usageType = cmd.getUsageType(); Long projectId = cmd.getProjectId(); - + if (projectId != null) { if (accountId != null) { - throw new InvalidParameterValueException("Projectid and accountId can't be specified together"); + throw new InvalidParameterValueException("Projectid and accountId can't be specified together", null); } Project project = _projectMgr.getProject(projectId); if (project == null) { - throw new InvalidParameterValueException("Unable to find project by id " + projectId); + throw new InvalidParameterValueException("Unable to find project by id", null); } accountId = project.getProjectAccountId(); } - + //if accountId is not specified, use accountName and domainId if ((accountId == null) && (accountName != null) && (domainId != null)) { if (_domainDao.isChildDomain(caller.getDomainId(), domainId)) { @@ -121,8 +122,10 @@ public class ManagementServerExtImpl extends ManagementServerImpl implements Man } if (userAccount != null) { accountId = userAccount.getId(); - } else { - throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain " + domainId); + } else { + List idList = new ArrayList(); + idList.add(new IdentityProxy("domain", domainId, "domainId")); + throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain " + domainId, idList); } } else { throw new PermissionDeniedException("Invalid Domain Id or Account"); @@ -130,7 +133,7 @@ public class ManagementServerExtImpl extends ManagementServerImpl implements Man } boolean isAdmin = false; - + //If accountId couldn't be found using accountName and domainId, get it from userContext if(accountId == null){ accountId = caller.getId(); @@ -145,7 +148,7 @@ public class ManagementServerExtImpl extends ManagementServerImpl implements Man Date startDate = cmd.getStartDate(); Date endDate = cmd.getEndDate(); if(startDate.after(endDate)){ - throw new InvalidParameterValueException("Incorrect Date Range. Start date: "+startDate+" is after end date:"+endDate); + throw new InvalidParameterValueException("Incorrect Date Range. Start date: "+startDate+" is after end date:"+endDate, null); } TimeZone usageTZ = getUsageTimezone(); Date adjustedStartDate = computeAdjustedTime(startDate, usageTZ, true); @@ -156,7 +159,7 @@ public class ManagementServerExtImpl extends ManagementServerImpl implements Man } Filter usageFilter = new Filter(UsageVO.class, "startDate", false, cmd.getStartIndex(), cmd.getPageSizeVal()); - + SearchCriteria sc = _usageDao.createSearchCriteria(); if (accountId != -1 && accountId != Account.ACCOUNT_ID_SYSTEM && !isAdmin) { @@ -166,7 +169,7 @@ public class ManagementServerExtImpl extends ManagementServerImpl implements Man if (domainId != null) { sc.addAnd("domainId", SearchCriteria.Op.EQ, domainId); } - + if (usageType != null) { sc.addAnd("usageType", SearchCriteria.Op.EQ, usageType); } @@ -241,8 +244,8 @@ public class ManagementServerExtImpl extends ManagementServerImpl implements Man return calTS.getTime(); } - @Override - public List listUsageTypes() { - return UsageTypes.listUsageTypes(); - } + @Override + public List listUsageTypes() { + return UsageTypes.listUsageTypes(); + } } diff --git a/server/src/com/cloud/storage/download/DownloadMonitorImpl.java b/server/src/com/cloud/storage/download/DownloadMonitorImpl.java index 60dbbd55924..7e1ae4e8b59 100755 --- a/server/src/com/cloud/storage/download/DownloadMonitorImpl.java +++ b/server/src/com/cloud/storage/download/DownloadMonitorImpl.java @@ -34,14 +34,14 @@ import com.cloud.agent.api.Command; import com.cloud.agent.api.storage.DeleteTemplateCommand; import com.cloud.agent.api.storage.DeleteVolumeCommand; import com.cloud.agent.api.storage.DownloadCommand; -import com.cloud.agent.api.storage.ListVolumeAnswer; -import com.cloud.agent.api.storage.ListVolumeCommand; import com.cloud.agent.api.storage.DownloadCommand.Proxy; import com.cloud.agent.api.storage.DownloadCommand.ResourceType; import com.cloud.agent.api.storage.DownloadProgressCommand; import com.cloud.agent.api.storage.DownloadProgressCommand.RequestType; import com.cloud.agent.api.storage.ListTemplateAnswer; import com.cloud.agent.api.storage.ListTemplateCommand; +import com.cloud.agent.api.storage.ListVolumeAnswer; +import com.cloud.agent.api.storage.ListVolumeCommand; import com.cloud.agent.manager.Commands; import com.cloud.alert.AlertManager; import com.cloud.configuration.Config; @@ -65,13 +65,11 @@ import com.cloud.storage.StorageManager; import com.cloud.storage.SwiftVO; import com.cloud.storage.VMTemplateHostVO; import com.cloud.storage.VMTemplateStorageResourceAssoc; -import com.cloud.storage.Volume; -import com.cloud.storage.VolumeHostVO; -import com.cloud.storage.VolumeVO; import com.cloud.storage.VMTemplateStorageResourceAssoc.Status; -import com.cloud.storage.Volume.Event; import com.cloud.storage.VMTemplateVO; import com.cloud.storage.VMTemplateZoneVO; +import com.cloud.storage.VolumeHostVO; +import com.cloud.storage.VolumeVO; import com.cloud.storage.dao.StoragePoolHostDao; import com.cloud.storage.dao.SwiftDao; import com.cloud.storage.dao.VMTemplateDao; @@ -94,12 +92,12 @@ import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; import com.cloud.utils.exception.CloudRuntimeException; -import com.cloud.utils.fsm.NoTransitionException; import com.cloud.vm.SecondaryStorageVm; import com.cloud.vm.SecondaryStorageVmVO; import com.cloud.vm.UserVmManager; import com.cloud.vm.VirtualMachine.State; import com.cloud.vm.dao.SecondaryStorageVmDao; + import edu.emory.mathcs.backport.java.util.Collections; @@ -111,13 +109,13 @@ import edu.emory.mathcs.backport.java.util.Collections; @Local(value={DownloadMonitor.class}) public class DownloadMonitorImpl implements DownloadMonitor { static final Logger s_logger = Logger.getLogger(DownloadMonitorImpl.class); - + @Inject VMTemplateHostDao _vmTemplateHostDao; @Inject VMTemplateZoneDao _vmTemplateZoneDao; @Inject - VMTemplatePoolDao _vmTemplatePoolDao; + VMTemplatePoolDao _vmTemplatePoolDao; @Inject VMTemplateSwiftDao _vmTemplateSwiftlDao; @Inject @@ -136,23 +134,23 @@ public class DownloadMonitorImpl implements DownloadMonitor { SecondaryStorageVmManager _ssvmMgr; @Inject StorageManager _storageMgr ; - + @Inject private final DataCenterDao _dcDao = null; @Inject VMTemplateDao _templateDao = null; @Inject - private AgentManager _agentMgr; + private AgentManager _agentMgr; @Inject SecondaryStorageVmManager _secMgr; @Inject ConfigurationDao _configDao; @Inject UserVmManager _vmMgr; - + @Inject private UsageEventDao _usageEventDao; - + @Inject private ClusterDao _clusterDao; @Inject @@ -164,38 +162,38 @@ public class DownloadMonitorImpl implements DownloadMonitor { @Inject protected ResourceLimitService _resourceLimitMgr; - private String _name; - private Boolean _sslCopy = new Boolean(false); - private String _copyAuthPasswd; - private String _proxy = null; + private String _name; + private Boolean _sslCopy = new Boolean(false); + private String _copyAuthPasswd; + private String _proxy = null; protected SearchBuilder ReadyTemplateStatesSearch; - Timer _timer; + Timer _timer; - final Map _listenerMap = new ConcurrentHashMap(); - final Map _listenerVolumeMap = new ConcurrentHashMap(); + final Map _listenerMap = new ConcurrentHashMap(); + final Map _listenerVolumeMap = new ConcurrentHashMap(); - public void send(Long hostId, Command cmd, Listener listener) throws AgentUnavailableException { - _agentMgr.send(hostId, new Commands(cmd), listener); - } + public void send(Long hostId, Command cmd, Listener listener) throws AgentUnavailableException { + _agentMgr.send(hostId, new Commands(cmd), listener); + } - @Override - public boolean configure(String name, Map params) { - _name = name; + @Override + public boolean configure(String name, Map params) { + _name = name; final Map configs = _configDao.getConfiguration("ManagementServer", params); _sslCopy = Boolean.parseBoolean(configs.get("secstorage.encrypt.copy")); _proxy = configs.get(Config.SecStorageProxy.key()); - + String cert = configs.get("secstorage.ssl.cert.domain"); if (!"realhostip.com".equalsIgnoreCase(cert)) { - s_logger.warn("Only realhostip.com ssl cert is supported, ignoring self-signed and other certs"); + s_logger.warn("Only realhostip.com ssl cert is supported, ignoring self-signed and other certs"); } - + _copyAuthPasswd = configs.get("secstorage.copy.password"); - + _agentMgr.registerForHostEvents(new DownloadListener(this), true, false, false); - + ReadyTemplateStatesSearch = _vmTemplateHostDao.createSearchBuilder(); ReadyTemplateStatesSearch.and("download_state", ReadyTemplateStatesSearch.entity().getDownloadState(), SearchCriteria.Op.EQ); ReadyTemplateStatesSearch.and("destroyed", ReadyTemplateStatesSearch.entity().getDestroyed(), SearchCriteria.Op.EQ); @@ -208,49 +206,49 @@ public class DownloadMonitorImpl implements DownloadMonitor { ReadyTemplateStatesSearch.entity().getTemplateId(), JoinBuilder.JoinType.INNER); TemplatesWithNoChecksumSearch.done(); ReadyTemplateStatesSearch.done(); - - return true; - } - @Override - public String getName() { - return _name; - } + return true; + } - @Override - public boolean start() { - _timer = new Timer(); - return true; - } + @Override + public String getName() { + return _name; + } - @Override - public boolean stop() { - return true; - } - - public boolean isTemplateUpdateable(Long templateId, Long hostId) { - List downloadsInProgress = - _vmTemplateHostDao.listByTemplateHostStatus(templateId.longValue(), hostId.longValue(), VMTemplateHostVO.Status.DOWNLOAD_IN_PROGRESS, VMTemplateHostVO.Status.DOWNLOADED); - return (downloadsInProgress.size() == 0); - } - - @Override + @Override + public boolean start() { + _timer = new Timer(); + return true; + } + + @Override + public boolean stop() { + return true; + } + + public boolean isTemplateUpdateable(Long templateId, Long hostId) { + List downloadsInProgress = + _vmTemplateHostDao.listByTemplateHostStatus(templateId.longValue(), hostId.longValue(), VMTemplateHostVO.Status.DOWNLOAD_IN_PROGRESS, VMTemplateHostVO.Status.DOWNLOADED); + return (downloadsInProgress.size() == 0); + } + + @Override public boolean copyTemplate(VMTemplateVO template, HostVO sourceServer, HostVO destServer) throws StorageUnavailableException{ - boolean downloadJobExists = false; + boolean downloadJobExists = false; VMTemplateHostVO destTmpltHost = null; VMTemplateHostVO srcTmpltHost = null; srcTmpltHost = _vmTemplateHostDao.findByHostTemplate(sourceServer.getId(), template.getId()); if (srcTmpltHost == null) { - throw new InvalidParameterValueException("Template " + template.getName() + " not associated with " + sourceServer.getName()); + throw new InvalidParameterValueException("Template " + template.getName() + " not associated with " + sourceServer.getName(), null); } String url = generateCopyUrl(sourceServer, srcTmpltHost); - if (url == null) { - s_logger.warn("Unable to start/resume copy of template " + template.getUniqueName() + " to " + destServer.getName() + ", no secondary storage vm in running state in source zone"); - throw new CloudRuntimeException("No secondary VM in running state in zone " + sourceServer.getDataCenterId()); - } + if (url == null) { + s_logger.warn("Unable to start/resume copy of template " + template.getUniqueName() + " to " + destServer.getName() + ", no secondary storage vm in running state in source zone"); + throw new CloudRuntimeException("No secondary VM in running state in zone " + sourceServer.getDataCenterId()); + } destTmpltHost = _vmTemplateHostDao.findByHostTemplate(destServer.getId(), template.getId()); if (destTmpltHost == null) { destTmpltHost = new VMTemplateHostVO(destServer.getId(), template.getId(), new Date(), 0, VMTemplateStorageResourceAssoc.Status.NOT_DOWNLOADED, null, null, "jobid0000", null, url); @@ -263,84 +261,84 @@ public class DownloadMonitorImpl implements DownloadMonitor { Long maxTemplateSizeInBytes = getMaxTemplateSizeInBytes(); if (srcTmpltHost.getSize() > maxTemplateSizeInBytes){ - throw new CloudRuntimeException("Cant copy the template as the template's size " +srcTmpltHost.getSize()+ - " is greater than max.template.iso.size " + maxTemplateSizeInBytes); + throw new CloudRuntimeException("Cant copy the template as the template's size " +srcTmpltHost.getSize()+ + " is greater than max.template.iso.size " + maxTemplateSizeInBytes); } - - if(destTmpltHost != null) { - start(); + + if(destTmpltHost != null) { + start(); String sourceChecksum = _vmMgr.getChecksum(srcTmpltHost.getHostId(), srcTmpltHost.getInstallPath()); - DownloadCommand dcmd = - new DownloadCommand(destServer.getStorageUrl(), url, template, TemplateConstants.DEFAULT_HTTP_AUTH_USER, _copyAuthPasswd, maxTemplateSizeInBytes); - dcmd.setProxy(getHttpProxy()); - if (downloadJobExists) { - dcmd = new DownloadProgressCommand(dcmd, destTmpltHost.getJobId(), RequestType.GET_OR_RESTART); - } - dcmd.setChecksum(sourceChecksum); // We need to set the checksum as the source template might be a compressed url and have cksum for compressed image. Bug #10775 + DownloadCommand dcmd = + new DownloadCommand(destServer.getStorageUrl(), url, template, TemplateConstants.DEFAULT_HTTP_AUTH_USER, _copyAuthPasswd, maxTemplateSizeInBytes); + dcmd.setProxy(getHttpProxy()); + if (downloadJobExists) { + dcmd = new DownloadProgressCommand(dcmd, destTmpltHost.getJobId(), RequestType.GET_OR_RESTART); + } + dcmd.setChecksum(sourceChecksum); // We need to set the checksum as the source template might be a compressed url and have cksum for compressed image. Bug #10775 HostVO ssAhost = _ssvmMgr.pickSsvmHost(destServer); if( ssAhost == null ) { - s_logger.warn("There is no secondary storage VM for secondary storage host " + destServer.getName()); - return false; + s_logger.warn("There is no secondary storage VM for secondary storage host " + destServer.getName()); + return false; } DownloadListener dl = new DownloadListener(ssAhost, destServer, template, _timer, _vmTemplateHostDao, destTmpltHost.getId(), this, dcmd, _templateDao); if (downloadJobExists) { dl.setCurrState(destTmpltHost.getDownloadState()); } - DownloadListener old = null; - synchronized (_listenerMap) { - old = _listenerMap.put(destTmpltHost, dl); - } - if( old != null ) { - old.abandon(); - } - - try { - send(ssAhost.getId(), dcmd, dl); - return true; - } catch (AgentUnavailableException e) { - s_logger.warn("Unable to start /resume COPY of template " + template.getUniqueName() + " to " + destServer.getName(), e); - dl.setDisconnected(); - dl.scheduleStatusCheck(RequestType.GET_OR_RESTART); - e.printStackTrace(); + DownloadListener old = null; + synchronized (_listenerMap) { + old = _listenerMap.put(destTmpltHost, dl); + } + if( old != null ) { + old.abandon(); } - } - - return false; - } - - private String generateCopyUrl(String ipAddress, String dir, String path){ - String hostname = ipAddress; - String scheme = "http"; - if (_sslCopy) { - hostname = ipAddress.replace(".", "-"); - hostname = hostname + ".realhostip.com"; - scheme = "https"; - } - return scheme + "://" + hostname + "/copy/SecStorage/" + dir + "/" + path; - } - - private String generateCopyUrl(HostVO sourceServer, VMTemplateHostVO srcTmpltHost) { - List ssVms = _secStorageVmDao.getSecStorageVmListInStates(SecondaryStorageVm.Role.templateProcessor, sourceServer.getDataCenterId(), State.Running); - if (ssVms.size() > 0) { - SecondaryStorageVmVO ssVm = ssVms.get(0); - if (ssVm.getPublicIpAddress() == null) { - s_logger.warn("A running secondary storage vm has a null public ip?"); - return null; - } - return generateCopyUrl(ssVm.getPublicIpAddress(), sourceServer.getParent(), srcTmpltHost.getInstallPath()); - } - - VMTemplateVO tmplt = _templateDao.findById(srcTmpltHost.getTemplateId()); - HypervisorType hyperType = tmplt.getHypervisorType(); - /*No secondary storage vm yet*/ - if (hyperType != null && hyperType == HypervisorType.KVM) { - return "file://" + sourceServer.getParent() + "/" + srcTmpltHost.getInstallPath(); - } - return null; - } - private void downloadTemplateToStorage(VMTemplateVO template, HostVO sserver) { - boolean downloadJobExists = false; + try { + send(ssAhost.getId(), dcmd, dl); + return true; + } catch (AgentUnavailableException e) { + s_logger.warn("Unable to start /resume COPY of template " + template.getUniqueName() + " to " + destServer.getName(), e); + dl.setDisconnected(); + dl.scheduleStatusCheck(RequestType.GET_OR_RESTART); + e.printStackTrace(); + } + } + + return false; + } + + private String generateCopyUrl(String ipAddress, String dir, String path){ + String hostname = ipAddress; + String scheme = "http"; + if (_sslCopy) { + hostname = ipAddress.replace(".", "-"); + hostname = hostname + ".realhostip.com"; + scheme = "https"; + } + return scheme + "://" + hostname + "/copy/SecStorage/" + dir + "/" + path; + } + + private String generateCopyUrl(HostVO sourceServer, VMTemplateHostVO srcTmpltHost) { + List ssVms = _secStorageVmDao.getSecStorageVmListInStates(SecondaryStorageVm.Role.templateProcessor, sourceServer.getDataCenterId(), State.Running); + if (ssVms.size() > 0) { + SecondaryStorageVmVO ssVm = ssVms.get(0); + if (ssVm.getPublicIpAddress() == null) { + s_logger.warn("A running secondary storage vm has a null public ip?"); + return null; + } + return generateCopyUrl(ssVm.getPublicIpAddress(), sourceServer.getParent(), srcTmpltHost.getInstallPath()); + } + + VMTemplateVO tmplt = _templateDao.findById(srcTmpltHost.getTemplateId()); + HypervisorType hyperType = tmplt.getHypervisorType(); + /*No secondary storage vm yet*/ + if (hyperType != null && hyperType == HypervisorType.KVM) { + return "file://" + sourceServer.getParent() + "/" + srcTmpltHost.getInstallPath(); + } + return null; + } + + private void downloadTemplateToStorage(VMTemplateVO template, HostVO sserver) { + boolean downloadJobExists = false; VMTemplateHostVO vmTemplateHost = null; vmTemplateHost = _vmTemplateHostDao.findByHostTemplate(sserver.getId(), template.getId()); @@ -350,29 +348,29 @@ public class DownloadMonitorImpl implements DownloadMonitor { } else if ((vmTemplateHost.getJobId() != null) && (vmTemplateHost.getJobId().length() > 2)) { downloadJobExists = true; } - + Long maxTemplateSizeInBytes = getMaxTemplateSizeInBytes(); String secUrl = sserver.getStorageUrl(); - if(vmTemplateHost != null) { - start(); - DownloadCommand dcmd = - new DownloadCommand(secUrl, template, maxTemplateSizeInBytes); - dcmd.setProxy(getHttpProxy()); - if (downloadJobExists) { - dcmd = new DownloadProgressCommand(dcmd, vmTemplateHost.getJobId(), RequestType.GET_OR_RESTART); - } - if (vmTemplateHost.isCopy()) { - dcmd.setCreds(TemplateConstants.DEFAULT_HTTP_AUTH_USER, _copyAuthPasswd); - } - HostVO ssAhost = _ssvmMgr.pickSsvmHost(sserver); - if( ssAhost == null ) { - s_logger.warn("There is no secondary storage VM for secondary storage host " + sserver.getName()); - return; - } - DownloadListener dl = new DownloadListener(ssAhost, sserver, template, _timer, _vmTemplateHostDao, vmTemplateHost.getId(), this, dcmd, _templateDao); - if (downloadJobExists) { - dl.setCurrState(vmTemplateHost.getDownloadState()); - } + if(vmTemplateHost != null) { + start(); + DownloadCommand dcmd = + new DownloadCommand(secUrl, template, maxTemplateSizeInBytes); + dcmd.setProxy(getHttpProxy()); + if (downloadJobExists) { + dcmd = new DownloadProgressCommand(dcmd, vmTemplateHost.getJobId(), RequestType.GET_OR_RESTART); + } + if (vmTemplateHost.isCopy()) { + dcmd.setCreds(TemplateConstants.DEFAULT_HTTP_AUTH_USER, _copyAuthPasswd); + } + HostVO ssAhost = _ssvmMgr.pickSsvmHost(sserver); + if( ssAhost == null ) { + s_logger.warn("There is no secondary storage VM for secondary storage host " + sserver.getName()); + return; + } + DownloadListener dl = new DownloadListener(ssAhost, sserver, template, _timer, _vmTemplateHostDao, vmTemplateHost.getId(), this, dcmd, _templateDao); + if (downloadJobExists) { + dl.setCurrState(vmTemplateHost.getDownloadState()); + } DownloadListener old = null; synchronized (_listenerMap) { old = _listenerMap.put(vmTemplateHost, dl); @@ -381,20 +379,20 @@ public class DownloadMonitorImpl implements DownloadMonitor { old.abandon(); } - try { - send(ssAhost.getId(), dcmd, dl); + try { + send(ssAhost.getId(), dcmd, dl); } catch (AgentUnavailableException e) { - s_logger.warn("Unable to start /resume download of template " + template.getUniqueName() + " to " + sserver.getName(), e); - dl.setDisconnected(); - dl.scheduleStatusCheck(RequestType.GET_OR_RESTART); + s_logger.warn("Unable to start /resume download of template " + template.getUniqueName() + " to " + sserver.getName(), e); + dl.setDisconnected(); + dl.scheduleStatusCheck(RequestType.GET_OR_RESTART); } - } - } + } + } - @Override - public boolean downloadTemplateToStorage(VMTemplateVO template, Long zoneId) { + @Override + public boolean downloadTemplateToStorage(VMTemplateVO template, Long zoneId) { List dcs = new ArrayList(); if (zoneId == null) { dcs.addAll(_dcDao.listAll()); @@ -404,65 +402,65 @@ public class DownloadMonitorImpl implements DownloadMonitor { long templateId = template.getId(); boolean isPublic = template.isFeatured() || template.isPublicTemplate(); for ( DataCenterVO dc : dcs ) { - List ssHosts = _ssvmMgr.listAllTypesSecondaryStorageHostsInOneZone(dc.getId()); - for ( HostVO ssHost : ssHosts ) { - if (isTemplateUpdateable(templateId, ssHost.getId())) { - initiateTemplateDownload(templateId, ssHost); - if (! isPublic ) { - break; - } - } - } - } - return true; - } - - @Override - public boolean downloadVolumeToStorage(VolumeVO volume, Long zoneId, String url, String checkSum, ImageFormat format) { - - List ssHosts = _ssvmMgr.listAllTypesSecondaryStorageHostsInOneZone(zoneId); - Collections.shuffle(ssHosts); - HostVO ssHost = ssHosts.get(0); - downloadVolumeToStorage(volume, ssHost, url, checkSum, format); - return true; - } - - private void downloadVolumeToStorage(VolumeVO volume, HostVO sserver, String url, String checkSum, ImageFormat format) { - boolean downloadJobExists = false; + List ssHosts = _ssvmMgr.listAllTypesSecondaryStorageHostsInOneZone(dc.getId()); + for ( HostVO ssHost : ssHosts ) { + if (isTemplateUpdateable(templateId, ssHost.getId())) { + initiateTemplateDownload(templateId, ssHost); + if (! isPublic ) { + break; + } + } + } + } + return true; + } + + @Override + public boolean downloadVolumeToStorage(VolumeVO volume, Long zoneId, String url, String checkSum, ImageFormat format) { + + List ssHosts = _ssvmMgr.listAllTypesSecondaryStorageHostsInOneZone(zoneId); + Collections.shuffle(ssHosts); + HostVO ssHost = ssHosts.get(0); + downloadVolumeToStorage(volume, ssHost, url, checkSum, format); + return true; + } + + private void downloadVolumeToStorage(VolumeVO volume, HostVO sserver, String url, String checkSum, ImageFormat format) { + boolean downloadJobExists = false; VolumeHostVO volumeHost = null; volumeHost = _volumeHostDao.findByHostVolume(sserver.getId(), volume.getId()); if (volumeHost == null) { volumeHost = new VolumeHostVO(sserver.getId(), volume.getId(), sserver.getDataCenterId(), new Date(), 0, VMTemplateStorageResourceAssoc.Status.NOT_DOWNLOADED, null, null, - "jobid0000", null, url, checkSum, format); + "jobid0000", null, url, checkSum, format); _volumeHostDao.persist(volumeHost); } else if ((volumeHost.getJobId() != null) && (volumeHost.getJobId().length() > 2)) { downloadJobExists = true; } - + Long maxVolumeSizeInBytes = getMaxVolumeSizeInBytes(); String secUrl = sserver.getStorageUrl(); - if(volumeHost != null) { - start(); - DownloadCommand dcmd = new DownloadCommand(secUrl, volume, maxVolumeSizeInBytes, checkSum, url, format); - dcmd.setProxy(getHttpProxy()); - if (downloadJobExists) { - dcmd = new DownloadProgressCommand(dcmd, volumeHost.getJobId(), RequestType.GET_OR_RESTART); - dcmd.setResourceType(ResourceType.VOLUME); - } - - HostVO ssvm = _ssvmMgr.pickSsvmHost(sserver); - if( ssvm == null ) { - s_logger.warn("There is no secondary storage VM for secondary storage host " + sserver.getName()); - return; - } - DownloadListener dl = new DownloadListener(ssvm, sserver, volume, _timer, _volumeHostDao, volumeHost.getId(), - this, dcmd, _volumeDao, _storageMgr); - - if (downloadJobExists) { - dl.setCurrState(volumeHost.getDownloadState()); - } + if(volumeHost != null) { + start(); + DownloadCommand dcmd = new DownloadCommand(secUrl, volume, maxVolumeSizeInBytes, checkSum, url, format); + dcmd.setProxy(getHttpProxy()); + if (downloadJobExists) { + dcmd = new DownloadProgressCommand(dcmd, volumeHost.getJobId(), RequestType.GET_OR_RESTART); + dcmd.setResourceType(ResourceType.VOLUME); + } + + HostVO ssvm = _ssvmMgr.pickSsvmHost(sserver); + if( ssvm == null ) { + s_logger.warn("There is no secondary storage VM for secondary storage host " + sserver.getName()); + return; + } + DownloadListener dl = new DownloadListener(ssvm, sserver, volume, _timer, _volumeHostDao, volumeHost.getId(), + this, dcmd, _volumeDao, _storageMgr); + + if (downloadJobExists) { + dl.setCurrState(volumeHost.getDownloadState()); + } DownloadListener old = null; synchronized (_listenerVolumeMap) { old = _listenerVolumeMap.put(volumeHost, dl); @@ -471,38 +469,38 @@ public class DownloadMonitorImpl implements DownloadMonitor { old.abandon(); } - try { - send(ssvm.getId(), dcmd, dl); + try { + send(ssvm.getId(), dcmd, dl); } catch (AgentUnavailableException e) { - s_logger.warn("Unable to start /resume download of volume " + volume.getName() + " to " + sserver.getName(), e); - dl.setDisconnected(); - dl.scheduleStatusCheck(RequestType.GET_OR_RESTART); + s_logger.warn("Unable to start /resume download of volume " + volume.getName() + " to " + sserver.getName(), e); + dl.setDisconnected(); + dl.scheduleStatusCheck(RequestType.GET_OR_RESTART); } - } - } + } + } - private void initiateTemplateDownload(Long templateId, HostVO ssHost) { - VMTemplateVO template = _templateDao.findById(templateId); - if (template != null && (template.getUrl() != null)) { - //find all storage hosts and tell them to initiate download - downloadTemplateToStorage(template, ssHost); - } - - } + private void initiateTemplateDownload(Long templateId, HostVO ssHost) { + VMTemplateVO template = _templateDao.findById(templateId); + if (template != null && (template.getUrl() != null)) { + //find all storage hosts and tell them to initiate download + downloadTemplateToStorage(template, ssHost); + } - @DB - public void handleDownloadEvent(HostVO host, VMTemplateVO template, Status dnldStatus) { - if ((dnldStatus == VMTemplateStorageResourceAssoc.Status.DOWNLOADED) || (dnldStatus==Status.ABANDONED)){ - VMTemplateHostVO vmTemplateHost = new VMTemplateHostVO(host.getId(), template.getId()); - synchronized (_listenerMap) { - _listenerMap.remove(vmTemplateHost); - } - } - - VMTemplateHostVO vmTemplateHost = _vmTemplateHostDao.findByHostTemplate(host.getId(), template.getId()); - - Transaction txn = Transaction.currentTxn(); + } + + @DB + public void handleDownloadEvent(HostVO host, VMTemplateVO template, Status dnldStatus) { + if ((dnldStatus == VMTemplateStorageResourceAssoc.Status.DOWNLOADED) || (dnldStatus==Status.ABANDONED)){ + VMTemplateHostVO vmTemplateHost = new VMTemplateHostVO(host.getId(), template.getId()); + synchronized (_listenerMap) { + _listenerMap.remove(vmTemplateHost); + } + } + + VMTemplateHostVO vmTemplateHost = _vmTemplateHostDao.findByHostTemplate(host.getId(), template.getId()); + + Transaction txn = Transaction.currentTxn(); txn.start(); if (dnldStatus == Status.DOWNLOADED) { @@ -523,25 +521,25 @@ public class DownloadMonitorImpl implements DownloadMonitor { } } txn.commit(); - } + } - @DB - public void handleDownloadEvent(HostVO host, VolumeVO volume, Status dnldStatus) { - if ((dnldStatus == VMTemplateStorageResourceAssoc.Status.DOWNLOADED) || (dnldStatus==Status.ABANDONED)){ - VolumeHostVO volumeHost = new VolumeHostVO(host.getId(), volume.getId()); - synchronized (_listenerVolumeMap) { - _listenerVolumeMap.remove(volumeHost); - } - } - - VolumeHostVO volumeHost = _volumeHostDao.findByHostVolume(host.getId(), volume.getId()); - - Transaction txn = Transaction.currentTxn(); + @DB + public void handleDownloadEvent(HostVO host, VolumeVO volume, Status dnldStatus) { + if ((dnldStatus == VMTemplateStorageResourceAssoc.Status.DOWNLOADED) || (dnldStatus==Status.ABANDONED)){ + VolumeHostVO volumeHost = new VolumeHostVO(host.getId(), volume.getId()); + synchronized (_listenerVolumeMap) { + _listenerVolumeMap.remove(volumeHost); + } + } + + VolumeHostVO volumeHost = _volumeHostDao.findByHostVolume(host.getId(), volume.getId()); + + Transaction txn = Transaction.currentTxn(); txn.start(); if (dnldStatus == Status.DOWNLOADED) { - - //Create usage event + + //Create usage event long size = -1; if(volumeHost!=null){ size = volumeHost.getPhysicalSize(); @@ -551,78 +549,78 @@ public class DownloadMonitorImpl implements DownloadMonitor { } String eventType = EventTypes.EVENT_VOLUME_UPLOAD; if(volume.getAccountId() != Account.ACCOUNT_ID_SYSTEM){ - UsageEventVO usageEvent = new UsageEventVO(eventType, volume.getAccountId(), host.getDataCenterId(), volume.getId(), volume.getName(), null, 0l , size); - _usageEventDao.persist(usageEvent); + UsageEventVO usageEvent = new UsageEventVO(eventType, volume.getAccountId(), host.getDataCenterId(), volume.getId(), volume.getName(), null, 0l , size); + _usageEventDao.persist(usageEvent); } }else if (dnldStatus == Status.DOWNLOAD_ERROR || dnldStatus == Status.ABANDONED || dnldStatus == Status.UNKNOWN){ //Decrement the volume count - _resourceLimitMgr.decrementResourceCount(volume.getAccountId(), com.cloud.configuration.Resource.ResourceType.volume); + _resourceLimitMgr.decrementResourceCount(volume.getAccountId(), com.cloud.configuration.Resource.ResourceType.volume); } txn.commit(); - } - - @Override - public void handleSysTemplateDownload(HostVO host) { - List hypers = _resourceMgr.listAvailHypervisorInZone(host.getId(), host.getDataCenterId()); - HypervisorType hostHyper = host.getHypervisorType(); - if (hypers.contains(hostHyper)) { - return; - } + } - Set toBeDownloaded = new HashSet(); - List ssHosts = _resourceMgr.listAllUpAndEnabledHostsInOneZoneByType(Host.Type.SecondaryStorage, host.getDataCenterId()); - if (ssHosts == null || ssHosts.isEmpty()) { - return; - } - /*Download all the templates in zone with the same hypervisortype*/ - for ( HostVO ssHost : ssHosts) { - List rtngTmplts = _templateDao.listAllSystemVMTemplates(); - List defaultBuiltin = _templateDao.listDefaultBuiltinTemplates(); - - - for (VMTemplateVO rtngTmplt : rtngTmplts) { - if (rtngTmplt.getHypervisorType() == hostHyper) { - toBeDownloaded.add(rtngTmplt); - } - } - - for (VMTemplateVO builtinTmplt : defaultBuiltin) { - if (builtinTmplt.getHypervisorType() == hostHyper) { - toBeDownloaded.add(builtinTmplt); - } - } - - for (VMTemplateVO template: toBeDownloaded) { - VMTemplateHostVO tmpltHost = _vmTemplateHostDao.findByHostTemplate(ssHost.getId(), template.getId()); - if (tmpltHost == null || tmpltHost.getDownloadState() != Status.DOWNLOADED) { - downloadTemplateToStorage(template, ssHost); - } - } + @Override + public void handleSysTemplateDownload(HostVO host) { + List hypers = _resourceMgr.listAvailHypervisorInZone(host.getId(), host.getDataCenterId()); + HypervisorType hostHyper = host.getHypervisorType(); + if (hypers.contains(hostHyper)) { + return; } - } - + + Set toBeDownloaded = new HashSet(); + List ssHosts = _resourceMgr.listAllUpAndEnabledHostsInOneZoneByType(Host.Type.SecondaryStorage, host.getDataCenterId()); + if (ssHosts == null || ssHosts.isEmpty()) { + return; + } + /*Download all the templates in zone with the same hypervisortype*/ + for ( HostVO ssHost : ssHosts) { + List rtngTmplts = _templateDao.listAllSystemVMTemplates(); + List defaultBuiltin = _templateDao.listDefaultBuiltinTemplates(); + + + for (VMTemplateVO rtngTmplt : rtngTmplts) { + if (rtngTmplt.getHypervisorType() == hostHyper) { + toBeDownloaded.add(rtngTmplt); + } + } + + for (VMTemplateVO builtinTmplt : defaultBuiltin) { + if (builtinTmplt.getHypervisorType() == hostHyper) { + toBeDownloaded.add(builtinTmplt); + } + } + + for (VMTemplateVO template: toBeDownloaded) { + VMTemplateHostVO tmpltHost = _vmTemplateHostDao.findByHostTemplate(ssHost.getId(), template.getId()); + if (tmpltHost == null || tmpltHost.getDownloadState() != Status.DOWNLOADED) { + downloadTemplateToStorage(template, ssHost); + } + } + } + } + @Override - public void addSystemVMTemplatesToHost(HostVO host, Map templateInfos){ - if ( templateInfos == null ) { - return; - } - Long hostId = host.getId(); - List rtngTmplts = _templateDao.listAllSystemVMTemplates(); - for ( VMTemplateVO tmplt : rtngTmplts ) { - TemplateInfo tmpltInfo = templateInfos.get(tmplt.getUniqueName()); - if ( tmpltInfo == null ) { - continue; - } - VMTemplateHostVO tmpltHost = _vmTemplateHostDao.findByHostTemplate(hostId, tmplt.getId()); - if ( tmpltHost == null ) { + public void addSystemVMTemplatesToHost(HostVO host, Map templateInfos){ + if ( templateInfos == null ) { + return; + } + Long hostId = host.getId(); + List rtngTmplts = _templateDao.listAllSystemVMTemplates(); + for ( VMTemplateVO tmplt : rtngTmplts ) { + TemplateInfo tmpltInfo = templateInfos.get(tmplt.getUniqueName()); + if ( tmpltInfo == null ) { + continue; + } + VMTemplateHostVO tmpltHost = _vmTemplateHostDao.findByHostTemplate(hostId, tmplt.getId()); + if ( tmpltHost == null ) { tmpltHost = new VMTemplateHostVO(hostId, tmplt.getId(), new Date(), 100, Status.DOWNLOADED, null, null, null, tmpltInfo.getInstallPath(), tmplt.getUrl()); - tmpltHost.setSize(tmpltInfo.getSize()); - tmpltHost.setPhysicalSize(tmpltInfo.getPhysicalSize()); - _vmTemplateHostDao.persist(tmpltHost); - } - } - } - + tmpltHost.setSize(tmpltInfo.getSize()); + tmpltHost.setPhysicalSize(tmpltInfo.getPhysicalSize()); + _vmTemplateHostDao.persist(tmpltHost); + } + } + } + @Override public void handleSync(Long dcId) { if (dcId != null) { @@ -633,7 +631,7 @@ public class DownloadMonitorImpl implements DownloadMonitor { } } } - + private Map listTemplate(HostVO ssHost) { ListTemplateCommand cmd = new ListTemplateCommand(ssHost.getStorageUrl()); Answer answer = _agentMgr.sendToSecStorage(ssHost, cmd); @@ -645,25 +643,25 @@ public class DownloadMonitorImpl implements DownloadMonitor { s_logger.debug("can not list template for secondary storage host " + ssHost.getId()); } } - + return null; } - + private Map listVolume(HostVO ssHost) { - ListVolumeCommand cmd = new ListVolumeCommand(ssHost.getStorageUrl()); + ListVolumeCommand cmd = new ListVolumeCommand(ssHost.getStorageUrl()); Answer answer = _agentMgr.sendToSecStorage(ssHost, cmd); if (answer != null && answer.getResult()) { - ListVolumeAnswer tanswer = (ListVolumeAnswer)answer; + ListVolumeAnswer tanswer = (ListVolumeAnswer)answer; return tanswer.getTemplateInfo(); } else { if (s_logger.isDebugEnabled()) { s_logger.debug("Can not list volumes for secondary storage host " + ssHost.getId()); } } - + return null; } - + private Map listTemplate(SwiftVO swift) { if (swift == null) { return null; @@ -680,7 +678,7 @@ public class DownloadMonitorImpl implements DownloadMonitor { } return null; } - + @Override public void handleVolumeSync(HostVO ssHost) { if (ssHost == null) { @@ -697,21 +695,21 @@ public class DownloadMonitorImpl implements DownloadMonitor { if (volumeInfos == null) { return; } - + List dbVolumes = _volumeHostDao.listBySecStorage(sserverId); List toBeDownloaded = new ArrayList(dbVolumes); for (VolumeHostVO volumeHost : dbVolumes){ - VolumeVO volume = _volumeDao.findById(volumeHost.getVolumeId()); - //Exists then don't download - if (volumeInfos.containsKey(volume.getId())){ + VolumeVO volume = _volumeDao.findById(volumeHost.getVolumeId()); + //Exists then don't download + if (volumeInfos.containsKey(volume.getId())){ TemplateInfo volInfo = volumeInfos.remove(volume.getId()); toBeDownloaded.remove(volumeHost); s_logger.info("Volume Sync found " + volume.getUuid() + " already in the volume host table"); if (volumeHost.getDownloadState() != Status.DOWNLOADED) { - volumeHost.setErrorString(""); + volumeHost.setErrorString(""); } if (volInfo.isCorrupted()) { - volumeHost.setDownloadState(Status.DOWNLOAD_ERROR); + volumeHost.setDownloadState(Status.DOWNLOAD_ERROR); String msg = "Volume " + volume.getUuid() + " is corrupted on secondary storage "; volumeHost.setErrorString(msg); s_logger.info("msg"); @@ -723,23 +721,23 @@ public class DownloadMonitorImpl implements DownloadMonitor { } } else { // Put them in right status - volumeHost.setDownloadPercent(100); - volumeHost.setDownloadState(Status.DOWNLOADED); - volumeHost.setInstallPath(volInfo.getInstallPath()); - volumeHost.setSize(volInfo.getSize()); - volumeHost.setPhysicalSize(volInfo.getPhysicalSize()); - volumeHost.setLastUpdated(new Date()); - _volumeHostDao.update(volumeHost.getId(), volumeHost); + volumeHost.setDownloadPercent(100); + volumeHost.setDownloadState(Status.DOWNLOADED); + volumeHost.setInstallPath(volInfo.getInstallPath()); + volumeHost.setSize(volInfo.getSize()); + volumeHost.setPhysicalSize(volInfo.getPhysicalSize()); + volumeHost.setLastUpdated(new Date()); + _volumeHostDao.update(volumeHost.getId(), volumeHost); } continue; - } - // Volume is not on secondary but we should download. - if (volumeHost.getDownloadState() != Status.DOWNLOADED) { + } + // Volume is not on secondary but we should download. + if (volumeHost.getDownloadState() != Status.DOWNLOADED) { s_logger.info("Volume Sync did not find " + volume.getName() + " ready on server " + sserverId + ", will request download to start/resume shortly"); toBeDownloaded.add(volumeHost); } } - + //Download volumes which haven't been downloaded yet. if (toBeDownloaded.size() > 0) { for (VolumeHostVO volumeHost : toBeDownloaded) { @@ -756,19 +754,19 @@ public class DownloadMonitorImpl implements DownloadMonitor { TemplateInfo vInfo = volumeInfos.get(uniqueName); DeleteVolumeCommand dtCommand = new DeleteVolumeCommand(ssHost.getStorageUrl(), vInfo.getInstallPath()); try { - _agentMgr.sendToSecStorage(ssHost, dtCommand, null); + _agentMgr.sendToSecStorage(ssHost, dtCommand, null); } catch (AgentUnavailableException e) { String err = "Failed to delete " + vInfo.getTemplateName() + " on secondary storage " + sserverId + " which isn't in the database"; s_logger.error(err); return; } - + String description = "Deleted volume " + vInfo.getTemplateName() + " on secondary storage " + sserverId + " since it isn't in the database"; s_logger.info(description); } } - - @Override + + @Override public void handleTemplateSync(HostVO ssHost) { if (ssHost == null) { s_logger.warn("Huh? ssHost is null"); @@ -891,10 +889,10 @@ public class DownloadMonitorImpl implements DownloadMonitor { /* Baremetal need not to download any template */ availHypers.remove(HypervisorType.BareMetal); availHypers.add(HypervisorType.None); // bug 9809: resume ISO - // download. + // download. for (VMTemplateVO tmplt : toBeDownloaded) { if (tmplt.getUrl() == null) { // If url is null we can't - // initiate the download + // initiate the download continue; } // if this is private template, and there is no record for this @@ -921,7 +919,7 @@ public class DownloadMonitorImpl implements DownloadMonitor { TemplateInfo tInfo = templateInfos.get(uniqueName); DeleteTemplateCommand dtCommand = new DeleteTemplateCommand(ssHost.getStorageUrl(), tInfo.getInstallPath()); try { - _agentMgr.sendToSecStorage(ssHost, dtCommand, null); + _agentMgr.sendToSecStorage(ssHost, dtCommand, null); } catch (AgentUnavailableException e) { String err = "Failed to delete " + tInfo.getTemplateName() + " on secondary storage " + sserverId + " which isn't in the database"; s_logger.error(err); @@ -933,25 +931,25 @@ public class DownloadMonitorImpl implements DownloadMonitor { } } - @Override - public void cancelAllDownloads(Long templateId) { - List downloadsInProgress = - _vmTemplateHostDao.listByTemplateStates(templateId, VMTemplateHostVO.Status.DOWNLOAD_IN_PROGRESS, VMTemplateHostVO.Status.NOT_DOWNLOADED); - if (downloadsInProgress.size() > 0){ - for (VMTemplateHostVO vmthvo: downloadsInProgress) { - DownloadListener dl = null; - synchronized (_listenerMap) { - dl = _listenerMap.remove(vmthvo); - } - if (dl != null) { - dl.abandon(); - s_logger.info("Stopping download of template " + templateId + " to storage server " + vmthvo.getHostId()); - } - } - } - } - - private void checksumSync(long hostId){ + @Override + public void cancelAllDownloads(Long templateId) { + List downloadsInProgress = + _vmTemplateHostDao.listByTemplateStates(templateId, VMTemplateHostVO.Status.DOWNLOAD_IN_PROGRESS, VMTemplateHostVO.Status.NOT_DOWNLOADED); + if (downloadsInProgress.size() > 0){ + for (VMTemplateHostVO vmthvo: downloadsInProgress) { + DownloadListener dl = null; + synchronized (_listenerMap) { + dl = _listenerMap.remove(vmthvo); + } + if (dl != null) { + dl.abandon(); + s_logger.info("Stopping download of template " + templateId + " to storage server " + vmthvo.getHostId()); + } + } + } + } + + private void checksumSync(long hostId){ SearchCriteria sc = ReadyTemplateStatesSearch.create(); sc.setParameters("download_state", com.cloud.storage.VMTemplateStorageResourceAssoc.Status.DOWNLOADED); sc.setParameters("host_id", hostId); @@ -967,36 +965,36 @@ public class DownloadMonitorImpl implements DownloadMonitor { _templateDao.update(template.getId(), template); } - } - - private Long getMaxTemplateSizeInBytes() { - try { - return Long.parseLong(_configDao.getValue("max.template.iso.size")) * 1024L * 1024L * 1024L; - } catch (NumberFormatException e) { - return null; - } - } - - private Long getMaxVolumeSizeInBytes() { - try { - return Long.parseLong(_configDao.getValue("storage.max.volume.upload.size")) * 1024L * 1024L * 1024L; - } catch (NumberFormatException e) { - return null; - } - } - - private Proxy getHttpProxy() { - if (_proxy == null) { - return null; - } - try { - URI uri = new URI(_proxy); - Proxy prx = new Proxy(uri); - return prx; - } catch (URISyntaxException e) { - return null; - } - } - + } + + private Long getMaxTemplateSizeInBytes() { + try { + return Long.parseLong(_configDao.getValue("max.template.iso.size")) * 1024L * 1024L * 1024L; + } catch (NumberFormatException e) { + return null; + } + } + + private Long getMaxVolumeSizeInBytes() { + try { + return Long.parseLong(_configDao.getValue("storage.max.volume.upload.size")) * 1024L * 1024L * 1024L; + } catch (NumberFormatException e) { + return null; + } + } + + private Proxy getHttpProxy() { + if (_proxy == null) { + return null; + } + try { + URI uri = new URI(_proxy); + Proxy prx = new Proxy(uri); + return prx; + } catch (URISyntaxException e) { + return null; + } + } + } - + diff --git a/server/src/com/cloud/template/HyervisorTemplateAdapter.java b/server/src/com/cloud/template/HyervisorTemplateAdapter.java deleted file mode 100755 index 870123e2dac..00000000000 --- a/server/src/com/cloud/template/HyervisorTemplateAdapter.java +++ /dev/null @@ -1,290 +0,0 @@ -// Copyright 2012 Citrix Systems, Inc. Licensed under the -// Apache License, Version 2.0 (the "License"); you may not use this -// file except in compliance with the License. Citrix Systems, Inc. -// reserves all rights not expressly granted by the License. -// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// Automatically generated by addcopyright.py at 04/03/2012 -package com.cloud.template; - -import java.net.Inet6Address; -import java.net.InetAddress; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.UnknownHostException; -import java.util.List; - -import javax.ejb.Local; - -import org.apache.log4j.Logger; - -import com.cloud.agent.AgentManager; -import com.cloud.agent.api.Answer; -import com.cloud.agent.api.storage.DeleteTemplateCommand; -import com.cloud.api.commands.DeleteIsoCmd; -import com.cloud.api.commands.DeleteTemplateCmd; -import com.cloud.api.commands.RegisterIsoCmd; -import com.cloud.api.commands.RegisterTemplateCmd; -import com.cloud.configuration.Resource.ResourceType; -import com.cloud.dc.DataCenterVO; -import com.cloud.event.EventTypes; -import com.cloud.event.UsageEventVO; -import com.cloud.exception.InvalidParameterValueException; -import com.cloud.exception.ResourceAllocationException; -import com.cloud.host.HostVO; -import com.cloud.storage.Storage.ImageFormat; -import com.cloud.storage.Storage.TemplateType; -import com.cloud.storage.VMTemplateHostVO; -import com.cloud.storage.VMTemplateStorageResourceAssoc.Status; -import com.cloud.storage.VMTemplateVO; -import com.cloud.storage.VMTemplateZoneVO; -import com.cloud.storage.download.DownloadMonitor; -import com.cloud.storage.secondary.SecondaryStorageVmManager; -import com.cloud.user.Account; -import com.cloud.utils.component.Inject; -import com.cloud.utils.db.DB; -import com.cloud.utils.exception.CloudRuntimeException; - -@Local(value=TemplateAdapter.class) -public class HyervisorTemplateAdapter extends TemplateAdapterBase implements TemplateAdapter { - private final static Logger s_logger = Logger.getLogger(HyervisorTemplateAdapter.class); - @Inject DownloadMonitor _downloadMonitor; - @Inject SecondaryStorageVmManager _ssvmMgr; - @Inject AgentManager _agentMgr; - - private String validateUrl(String url) { - try { - URI uri = new URI(url); - if ((uri.getScheme() == null) || (!uri.getScheme().equalsIgnoreCase("http") - && !uri.getScheme().equalsIgnoreCase("https") && !uri.getScheme().equalsIgnoreCase("file"))) { - throw new IllegalArgumentException("Unsupported scheme for url: " + url); - } - - int port = uri.getPort(); - if (!(port == 80 || port == 443 || port == -1)) { - throw new IllegalArgumentException("Only ports 80 and 443 are allowed"); - } - String host = uri.getHost(); - try { - InetAddress hostAddr = InetAddress.getByName(host); - if (hostAddr.isAnyLocalAddress() || hostAddr.isLinkLocalAddress() || hostAddr.isLoopbackAddress() || hostAddr.isMulticastAddress()) { - throw new IllegalArgumentException("Illegal host specified in url"); - } - if (hostAddr instanceof Inet6Address) { - throw new IllegalArgumentException("IPV6 addresses not supported (" + hostAddr.getHostAddress() + ")"); - } - } catch (UnknownHostException uhe) { - throw new IllegalArgumentException("Unable to resolve " + host); - } - - return uri.toString(); - } catch (URISyntaxException e) { - throw new IllegalArgumentException("Invalid URL " + url); - } - } - - @Override - public TemplateProfile prepare(RegisterIsoCmd cmd) throws ResourceAllocationException { - TemplateProfile profile = super.prepare(cmd); - String url = profile.getUrl(); - - if((!url.toLowerCase().endsWith("iso"))&&(!url.toLowerCase().endsWith("iso.zip"))&&(!url.toLowerCase().endsWith("iso.bz2")) - &&(!url.toLowerCase().endsWith("iso.gz"))){ - throw new InvalidParameterValueException("Please specify a valid iso"); - } - - profile.setUrl(validateUrl(url)); - return profile; - } - - @Override - public TemplateProfile prepare(RegisterTemplateCmd cmd) throws ResourceAllocationException { - TemplateProfile profile = super.prepare(cmd); - String url = profile.getUrl(); - - if((!url.toLowerCase().endsWith("vhd"))&&(!url.toLowerCase().endsWith("vhd.zip")) - &&(!url.toLowerCase().endsWith("vhd.bz2"))&&(!url.toLowerCase().endsWith("vhd.gz")) - &&(!url.toLowerCase().endsWith("qcow2"))&&(!url.toLowerCase().endsWith("qcow2.zip")) - &&(!url.toLowerCase().endsWith("qcow2.bz2"))&&(!url.toLowerCase().endsWith("qcow2.gz")) - &&(!url.toLowerCase().endsWith("ova"))&&(!url.toLowerCase().endsWith("ova.zip")) - &&(!url.toLowerCase().endsWith("ova.bz2"))&&(!url.toLowerCase().endsWith("ova.gz")) - &&(!url.toLowerCase().endsWith("img"))&&(!url.toLowerCase().endsWith("raw"))){ - throw new InvalidParameterValueException("Please specify a valid "+ cmd.getFormat().toLowerCase()); - } - - if ((cmd.getFormat().equalsIgnoreCase("vhd") && (!url.toLowerCase().endsWith("vhd") && !url.toLowerCase().endsWith("vhd.zip") && !url.toLowerCase().endsWith("vhd.bz2") && !url.toLowerCase().endsWith("vhd.gz") )) - || (cmd.getFormat().equalsIgnoreCase("qcow2") && (!url.toLowerCase().endsWith("qcow2") && !url.toLowerCase().endsWith("qcow2.zip") && !url.toLowerCase().endsWith("qcow2.bz2") && !url.toLowerCase().endsWith("qcow2.gz") )) - || (cmd.getFormat().equalsIgnoreCase("ova") && (!url.toLowerCase().endsWith("ova") && !url.toLowerCase().endsWith("ova.zip") && !url.toLowerCase().endsWith("ova.bz2") && !url.toLowerCase().endsWith("ova.gz"))) - || (cmd.getFormat().equalsIgnoreCase("raw") && (!url.toLowerCase().endsWith("img") && !url.toLowerCase().endsWith("raw")))) { - throw new InvalidParameterValueException("Please specify a valid URL. URL:" + url + " is an invalid for the format " + cmd.getFormat().toLowerCase()); - } - - profile.setUrl(validateUrl(url)); - return profile; - } - - @Override - public VMTemplateVO create(TemplateProfile profile) { - VMTemplateVO template = persistTemplate(profile); - - if (template == null) { - throw new CloudRuntimeException("Unable to persist the template " + profile.getTemplate()); - } - - _downloadMonitor.downloadTemplateToStorage(template, profile.getZoneId()); - _resourceLimitMgr.incrementResourceCount(profile.getAccountId(), ResourceType.template); - - return template; - } - - @Override @DB - public boolean delete(TemplateProfile profile) { - boolean success = true; - - VMTemplateVO template = profile.getTemplate(); - Long zoneId = profile.getZoneId(); - Long templateId = template.getId(); - - String zoneName; - List secondaryStorageHosts; - if (!template.isCrossZones() && zoneId != null) { - DataCenterVO zone = _dcDao.findById(zoneId); - zoneName = zone.getName(); - secondaryStorageHosts = _ssvmMgr.listSecondaryStorageHostsInOneZone(zoneId); - } else { - zoneName = "(all zones)"; - secondaryStorageHosts = _ssvmMgr.listSecondaryStorageHostsInAllZones(); - } - - s_logger.debug("Attempting to mark template host refs for template: " + template.getName() + " as destroyed in zone: " + zoneName); - - // Make sure the template is downloaded to all the necessary secondary storage hosts - for (HostVO secondaryStorageHost : secondaryStorageHosts) { - long hostId = secondaryStorageHost.getId(); - List templateHostVOs = _tmpltHostDao.listByHostTemplate(hostId, templateId); - for (VMTemplateHostVO templateHostVO : templateHostVOs) { - if (templateHostVO.getDownloadState() == Status.DOWNLOAD_IN_PROGRESS) { - String errorMsg = "Please specify a template that is not currently being downloaded."; - s_logger.debug("Template: " + template.getName() + " is currently being downloaded to secondary storage host: " + secondaryStorageHost.getName() + "; cant' delete it."); - throw new CloudRuntimeException(errorMsg); - } - } - } - - Account account = _accountDao.findByIdIncludingRemoved(template.getAccountId()); - String eventType = ""; - - if (template.getFormat().equals(ImageFormat.ISO)){ - eventType = EventTypes.EVENT_ISO_DELETE; - } else { - eventType = EventTypes.EVENT_TEMPLATE_DELETE; - } - - // Iterate through all necessary secondary storage hosts and mark the template on each host as destroyed - for (HostVO secondaryStorageHost : secondaryStorageHosts) { - long hostId = secondaryStorageHost.getId(); - long sZoneId = secondaryStorageHost.getDataCenterId(); - List templateHostVOs = _tmpltHostDao.listByHostTemplate(hostId, templateId); - for (VMTemplateHostVO templateHostVO : templateHostVOs) { - VMTemplateHostVO lock = _tmpltHostDao.acquireInLockTable(templateHostVO.getId()); - try { - if (lock == null) { - s_logger.debug("Failed to acquire lock when deleting templateHostVO with ID: " + templateHostVO.getId()); - success = false; - break; - } - UsageEventVO usageEvent = new UsageEventVO(eventType, account.getId(), sZoneId, templateId, null); - _usageEventDao.persist(usageEvent); - templateHostVO.setDestroyed(true); - _tmpltHostDao.update(templateHostVO.getId(), templateHostVO); - String installPath = templateHostVO.getInstallPath(); - if (installPath != null) { - Answer answer = _agentMgr.sendToSecStorage(secondaryStorageHost, new DeleteTemplateCommand(secondaryStorageHost.getStorageUrl(), installPath)); - - if (answer == null || !answer.getResult()) { - s_logger.debug("Failed to delete " + templateHostVO + " due to " + ((answer == null) ? "answer is null" : answer.getDetails())); - } else { - _tmpltHostDao.remove(templateHostVO.getId()); - s_logger.debug("Deleted template at: " + installPath); - } - } else { - _tmpltHostDao.remove(templateHostVO.getId()); - } - VMTemplateZoneVO templateZone = _tmpltZoneDao.findByZoneTemplate(sZoneId, templateId); - - if (templateZone != null) { - _tmpltZoneDao.remove(templateZone.getId()); - } - } finally { - if (lock != null) { - _tmpltHostDao.releaseFromLockTable(lock.getId()); - } - } - } - - if (!success) { - break; - } - } - - s_logger.debug("Successfully marked template host refs for template: " + template.getName() + " as destroyed in zone: " + zoneName); - - // If there are no more non-destroyed template host entries for this template, delete it - if (success && (_tmpltHostDao.listByTemplateId(templateId).size() == 0)) { - long accountId = template.getAccountId(); - - VMTemplateVO lock = _tmpltDao.acquireInLockTable(templateId); - - try { - if (lock == null) { - s_logger.debug("Failed to acquire lock when deleting template with ID: " + templateId); - success = false; - } else if (_tmpltDao.remove(templateId)) { - // Decrement the number of templates - _resourceLimitMgr.decrementResourceCount(accountId, ResourceType.template); - } - - } finally { - if (lock != null) { - _tmpltDao.releaseFromLockTable(lock.getId()); - } - } - - s_logger.debug("Removed template: " + template.getName() + " because all of its template host refs were marked as destroyed."); - } - - return success; - } - - public TemplateProfile prepareDelete(DeleteTemplateCmd cmd) { - TemplateProfile profile = super.prepareDelete(cmd); - VMTemplateVO template = profile.getTemplate(); - Long zoneId = profile.getZoneId(); - - if (template.getTemplateType() == TemplateType.SYSTEM) { - throw new InvalidParameterValueException("The DomR template cannot be deleted."); - } - - if (zoneId != null && (_ssvmMgr.findSecondaryStorageHost(zoneId) == null)) { - throw new InvalidParameterValueException("Failed to find a secondary storage host in the specified zone."); - } - - return profile; - } - - public TemplateProfile prepareDelete(DeleteIsoCmd cmd) { - TemplateProfile profile = super.prepareDelete(cmd); - Long zoneId = profile.getZoneId(); - - if (zoneId != null && (_ssvmMgr.findSecondaryStorageHost(zoneId) == null)) { - throw new InvalidParameterValueException("Failed to find a secondary storage host in the specified zone."); - } - - return profile; - } -} diff --git a/server/src/com/cloud/template/HypervisorTemplateAdapter.java b/server/src/com/cloud/template/HypervisorTemplateAdapter.java new file mode 100755 index 00000000000..12f7fbc3d55 --- /dev/null +++ b/server/src/com/cloud/template/HypervisorTemplateAdapter.java @@ -0,0 +1,292 @@ +// Copyright 2012 Citrix Systems, Inc. Licensed under the +// Apache License, Version 2.0 (the "License"); you may not use this +// file except in compliance with the License. Citrix Systems, Inc. +// reserves all rights not expressly granted by the License. +// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Automatically generated by addcopyright.py at 04/03/2012 +package com.cloud.template; + +import java.net.Inet6Address; +import java.net.InetAddress; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.UnknownHostException; +import java.util.List; + +import javax.ejb.Local; + +import org.apache.log4j.Logger; + +import com.cloud.agent.AgentManager; +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.storage.DeleteTemplateCommand; +import com.cloud.api.commands.DeleteIsoCmd; +import com.cloud.api.commands.DeleteTemplateCmd; +import com.cloud.api.commands.RegisterIsoCmd; +import com.cloud.api.commands.RegisterTemplateCmd; +import com.cloud.configuration.Resource.ResourceType; +import com.cloud.dc.DataCenterVO; +import com.cloud.event.EventTypes; +import com.cloud.event.UsageEventVO; +import com.cloud.exception.InvalidParameterValueException; +import com.cloud.exception.ResourceAllocationException; +import com.cloud.host.HostVO; +import com.cloud.storage.Storage.ImageFormat; +import com.cloud.storage.Storage.TemplateType; +import com.cloud.storage.VMTemplateHostVO; +import com.cloud.storage.VMTemplateStorageResourceAssoc.Status; +import com.cloud.storage.VMTemplateVO; +import com.cloud.storage.VMTemplateZoneVO; +import com.cloud.storage.download.DownloadMonitor; +import com.cloud.storage.secondary.SecondaryStorageVmManager; +import com.cloud.user.Account; +import com.cloud.utils.component.Inject; +import com.cloud.utils.db.DB; +import com.cloud.utils.exception.CloudRuntimeException; + +@Local(value=TemplateAdapter.class) +public class HypervisorTemplateAdapter extends TemplateAdapterBase implements TemplateAdapter { + private final static Logger s_logger = Logger.getLogger(HypervisorTemplateAdapter.class); + @Inject DownloadMonitor _downloadMonitor; + @Inject SecondaryStorageVmManager _ssvmMgr; + @Inject AgentManager _agentMgr; + + private String validateUrl(String url) { + try { + URI uri = new URI(url); + if ((uri.getScheme() == null) || (!uri.getScheme().equalsIgnoreCase("http") + && !uri.getScheme().equalsIgnoreCase("https") && !uri.getScheme().equalsIgnoreCase("file"))) { + throw new IllegalArgumentException("Unsupported scheme for url: " + url); + } + + int port = uri.getPort(); + if (!(port == 80 || port == 443 || port == -1)) { + throw new IllegalArgumentException("Only ports 80 and 443 are allowed"); + } + String host = uri.getHost(); + try { + InetAddress hostAddr = InetAddress.getByName(host); + if (hostAddr.isAnyLocalAddress() || hostAddr.isLinkLocalAddress() || hostAddr.isLoopbackAddress() || hostAddr.isMulticastAddress()) { + throw new IllegalArgumentException("Illegal host specified in url"); + } + if (hostAddr instanceof Inet6Address) { + throw new IllegalArgumentException("IPV6 addresses not supported (" + hostAddr.getHostAddress() + ")"); + } + } catch (UnknownHostException uhe) { + throw new IllegalArgumentException("Unable to resolve " + host); + } + + return uri.toString(); + } catch (URISyntaxException e) { + throw new IllegalArgumentException("Invalid URL " + url); + } + } + + @Override + public TemplateProfile prepare(RegisterIsoCmd cmd) throws ResourceAllocationException { + TemplateProfile profile = super.prepare(cmd); + String url = profile.getUrl(); + + if((!url.toLowerCase().endsWith("iso"))&&(!url.toLowerCase().endsWith("iso.zip"))&&(!url.toLowerCase().endsWith("iso.bz2")) + &&(!url.toLowerCase().endsWith("iso.gz"))){ + throw new InvalidParameterValueException("Please specify a valid iso", null); + } + + profile.setUrl(validateUrl(url)); + return profile; + } + + @Override + public TemplateProfile prepare(RegisterTemplateCmd cmd) throws ResourceAllocationException { + TemplateProfile profile = super.prepare(cmd); + String url = profile.getUrl(); + + if((!url.toLowerCase().endsWith("vhd"))&&(!url.toLowerCase().endsWith("vhd.zip")) + &&(!url.toLowerCase().endsWith("vhd.bz2"))&&(!url.toLowerCase().endsWith("vhd.gz")) + &&(!url.toLowerCase().endsWith("qcow2"))&&(!url.toLowerCase().endsWith("qcow2.zip")) + &&(!url.toLowerCase().endsWith("qcow2.bz2"))&&(!url.toLowerCase().endsWith("qcow2.gz")) + &&(!url.toLowerCase().endsWith("ova"))&&(!url.toLowerCase().endsWith("ova.zip")) + &&(!url.toLowerCase().endsWith("ova.bz2"))&&(!url.toLowerCase().endsWith("ova.gz")) + &&(!url.toLowerCase().endsWith("img"))&&(!url.toLowerCase().endsWith("raw"))){ + throw new InvalidParameterValueException("Please specify a valid "+ cmd.getFormat().toLowerCase(), null); + } + + if ((cmd.getFormat().equalsIgnoreCase("vhd") && (!url.toLowerCase().endsWith("vhd") && !url.toLowerCase().endsWith("vhd.zip") && !url.toLowerCase().endsWith("vhd.bz2") && !url.toLowerCase().endsWith("vhd.gz") )) + || (cmd.getFormat().equalsIgnoreCase("qcow2") && (!url.toLowerCase().endsWith("qcow2") && !url.toLowerCase().endsWith("qcow2.zip") && !url.toLowerCase().endsWith("qcow2.bz2") && !url.toLowerCase().endsWith("qcow2.gz") )) + || (cmd.getFormat().equalsIgnoreCase("ova") && (!url.toLowerCase().endsWith("ova") && !url.toLowerCase().endsWith("ova.zip") && !url.toLowerCase().endsWith("ova.bz2") && !url.toLowerCase().endsWith("ova.gz"))) + || (cmd.getFormat().equalsIgnoreCase("raw") && (!url.toLowerCase().endsWith("img") && !url.toLowerCase().endsWith("raw")))) { + throw new InvalidParameterValueException("Please specify a valid URL. URL:" + url + " is an invalid for the format " + cmd.getFormat().toLowerCase(), null); + } + + profile.setUrl(validateUrl(url)); + return profile; + } + + @Override + public VMTemplateVO create(TemplateProfile profile) { + VMTemplateVO template = persistTemplate(profile); + + if (template == null) { + throw new CloudRuntimeException("Unable to persist the template " + profile.getTemplate()); + } + + _downloadMonitor.downloadTemplateToStorage(template, profile.getZoneId()); + _resourceLimitMgr.incrementResourceCount(profile.getAccountId(), ResourceType.template); + + return template; + } + + @Override @DB + public boolean delete(TemplateProfile profile) { + boolean success = true; + + VMTemplateVO template = profile.getTemplate(); + Long zoneId = profile.getZoneId(); + Long templateId = template.getId(); + + String zoneName; + List secondaryStorageHosts; + if (!template.isCrossZones() && zoneId != null) { + DataCenterVO zone = _dcDao.findById(zoneId); + zoneName = zone.getName(); + secondaryStorageHosts = _ssvmMgr.listSecondaryStorageHostsInOneZone(zoneId); + } else { + zoneName = "(all zones)"; + secondaryStorageHosts = _ssvmMgr.listSecondaryStorageHostsInAllZones(); + } + + s_logger.debug("Attempting to mark template host refs for template: " + template.getName() + " as destroyed in zone: " + zoneName); + + // Make sure the template is downloaded to all the necessary secondary storage hosts + for (HostVO secondaryStorageHost : secondaryStorageHosts) { + long hostId = secondaryStorageHost.getId(); + List templateHostVOs = _tmpltHostDao.listByHostTemplate(hostId, templateId); + for (VMTemplateHostVO templateHostVO : templateHostVOs) { + if (templateHostVO.getDownloadState() == Status.DOWNLOAD_IN_PROGRESS) { + String errorMsg = "Please specify a template that is not currently being downloaded."; + s_logger.debug("Template: " + template.getName() + " is currently being downloaded to secondary storage host: " + secondaryStorageHost.getName() + "; cant' delete it."); + throw new CloudRuntimeException(errorMsg); + } + } + } + + Account account = _accountDao.findByIdIncludingRemoved(template.getAccountId()); + String eventType = ""; + + if (template.getFormat().equals(ImageFormat.ISO)){ + eventType = EventTypes.EVENT_ISO_DELETE; + } else { + eventType = EventTypes.EVENT_TEMPLATE_DELETE; + } + + // Iterate through all necessary secondary storage hosts and mark the template on each host as destroyed + for (HostVO secondaryStorageHost : secondaryStorageHosts) { + long hostId = secondaryStorageHost.getId(); + long sZoneId = secondaryStorageHost.getDataCenterId(); + List templateHostVOs = _tmpltHostDao.listByHostTemplate(hostId, templateId); + for (VMTemplateHostVO templateHostVO : templateHostVOs) { + VMTemplateHostVO lock = _tmpltHostDao.acquireInLockTable(templateHostVO.getId()); + try { + if (lock == null) { + s_logger.debug("Failed to acquire lock when deleting templateHostVO with ID: " + templateHostVO.getId()); + success = false; + break; + } + UsageEventVO usageEvent = new UsageEventVO(eventType, account.getId(), sZoneId, templateId, null); + _usageEventDao.persist(usageEvent); + templateHostVO.setDestroyed(true); + _tmpltHostDao.update(templateHostVO.getId(), templateHostVO); + String installPath = templateHostVO.getInstallPath(); + if (installPath != null) { + Answer answer = _agentMgr.sendToSecStorage(secondaryStorageHost, new DeleteTemplateCommand(secondaryStorageHost.getStorageUrl(), installPath)); + + if (answer == null || !answer.getResult()) { + s_logger.debug("Failed to delete " + templateHostVO + " due to " + ((answer == null) ? "answer is null" : answer.getDetails())); + } else { + _tmpltHostDao.remove(templateHostVO.getId()); + s_logger.debug("Deleted template at: " + installPath); + } + } else { + _tmpltHostDao.remove(templateHostVO.getId()); + } + VMTemplateZoneVO templateZone = _tmpltZoneDao.findByZoneTemplate(sZoneId, templateId); + + if (templateZone != null) { + _tmpltZoneDao.remove(templateZone.getId()); + } + } finally { + if (lock != null) { + _tmpltHostDao.releaseFromLockTable(lock.getId()); + } + } + } + + if (!success) { + break; + } + } + + s_logger.debug("Successfully marked template host refs for template: " + template.getName() + " as destroyed in zone: " + zoneName); + + // If there are no more non-destroyed template host entries for this template, delete it + if (success && (_tmpltHostDao.listByTemplateId(templateId).size() == 0)) { + long accountId = template.getAccountId(); + + VMTemplateVO lock = _tmpltDao.acquireInLockTable(templateId); + + try { + if (lock == null) { + s_logger.debug("Failed to acquire lock when deleting template with ID: " + templateId); + success = false; + } else if (_tmpltDao.remove(templateId)) { + // Decrement the number of templates + _resourceLimitMgr.decrementResourceCount(accountId, ResourceType.template); + } + + } finally { + if (lock != null) { + _tmpltDao.releaseFromLockTable(lock.getId()); + } + } + + s_logger.debug("Removed template: " + template.getName() + " because all of its template host refs were marked as destroyed."); + } + + return success; + } + + @Override + public TemplateProfile prepareDelete(DeleteTemplateCmd cmd) { + TemplateProfile profile = super.prepareDelete(cmd); + VMTemplateVO template = profile.getTemplate(); + Long zoneId = profile.getZoneId(); + + if (template.getTemplateType() == TemplateType.SYSTEM) { + throw new InvalidParameterValueException("The DomR template cannot be deleted.", null); + } + + if (zoneId != null && (_ssvmMgr.findSecondaryStorageHost(zoneId) == null)) { + throw new InvalidParameterValueException("Failed to find a secondary storage host in the specified zone.", null); + } + + return profile; + } + + @Override + public TemplateProfile prepareDelete(DeleteIsoCmd cmd) { + TemplateProfile profile = super.prepareDelete(cmd); + Long zoneId = profile.getZoneId(); + + if (zoneId != null && (_ssvmMgr.findSecondaryStorageHost(zoneId) == null)) { + throw new InvalidParameterValueException("Failed to find a secondary storage host in the specified zone.", null); + } + + return profile; + } +} diff --git a/server/src/com/cloud/template/TemplateManagerImpl.java b/server/src/com/cloud/template/TemplateManagerImpl.java index adc7a416137..64e601f867e 100755 --- a/server/src/com/cloud/template/TemplateManagerImpl.java +++ b/server/src/com/cloud/template/TemplateManagerImpl.java @@ -127,6 +127,7 @@ import com.cloud.user.dao.AccountDao; import com.cloud.user.dao.UserAccountDao; import com.cloud.user.dao.UserDao; import com.cloud.uservm.UserVm; +import com.cloud.utils.IdentityProxy; import com.cloud.utils.NumbersUtil; import com.cloud.utils.component.Adapters; import com.cloud.utils.component.ComponentLocator; @@ -194,46 +195,46 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe @Inject LaunchPermissionDao _launchPermissionDao; @Inject ProjectManager _projectMgr; - + int _primaryStorageDownloadWait; protected SearchBuilder HostTemplateStatesSearch; - + int _storagePoolMaxWaitSeconds = 3600; boolean _disableExtraction = false; ExecutorService _preloadExecutor; ScheduledExecutorService _swiftTemplateSyncExecutor; - + @Inject (adapter=TemplateAdapter.class) protected Adapters _adapters; - + private TemplateAdapter getAdapter(HypervisorType type) { - TemplateAdapter adapter = null; - if (type == HypervisorType.BareMetal) { - adapter = _adapters.get(TemplateAdapterType.BareMetal.getName()); - } else { - // see HyervisorTemplateAdapter - adapter = _adapters.get(TemplateAdapterType.Hypervisor.getName()); - } - - if (adapter == null) { - throw new CloudRuntimeException("Cannot find template adapter for " + type.toString()); - } - - return adapter; + TemplateAdapter adapter = null; + if (type == HypervisorType.BareMetal) { + adapter = _adapters.get(TemplateAdapterType.BareMetal.getName()); + } else { + // see HyervisorTemplateAdapter + adapter = _adapters.get(TemplateAdapterType.Hypervisor.getName()); + } + + if (adapter == null) { + throw new CloudRuntimeException("Cannot find template adapter for " + type.toString()); + } + + return adapter; } - + @Override @ActionEvent(eventType = EventTypes.EVENT_ISO_CREATE, eventDescription = "creating iso") public VirtualMachineTemplate registerIso(RegisterIsoCmd cmd) throws ResourceAllocationException{ - TemplateAdapter adapter = getAdapter(HypervisorType.None); - TemplateProfile profile = adapter.prepare(cmd); - VMTemplateVO template = adapter.create(profile); - - if (template != null){ - return template; + TemplateAdapter adapter = getAdapter(HypervisorType.None); + TemplateProfile profile = adapter.prepare(cmd); + VMTemplateVO template = adapter.create(profile); + + if (template != null){ + return template; }else { - throw new CloudRuntimeException("Failed to create ISO"); + throw new CloudRuntimeException("Failed to create ISO"); } } @@ -246,14 +247,14 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe throw new PermissionDeniedException("Parameter templatetag can only be specified by a Root Admin, permission denied"); } } - TemplateAdapter adapter = getAdapter(HypervisorType.getType(cmd.getHypervisor())); - TemplateProfile profile = adapter.prepare(cmd); - VMTemplateVO template = adapter.create(profile); - - if (template != null){ - return template; + TemplateAdapter adapter = getAdapter(HypervisorType.getType(cmd.getHypervisor())); + TemplateProfile profile = adapter.prepare(cmd); + VMTemplateVO template = adapter.create(profile); + + if (template != null){ + return template; }else { - throw new CloudRuntimeException("Failed to create a template"); + throw new CloudRuntimeException("Failed to create a template"); } } @@ -266,13 +267,13 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe String url = cmd.getUrl(); String mode = cmd.getMode(); Long eventId = cmd.getStartEventId(); - + // FIXME: async job needs fixing Long uploadId = extract(account, templateId, url, zoneId, mode, eventId, true, null, _asyncMgr); if (uploadId != null){ - return uploadId; + return uploadId; }else { - throw new CloudRuntimeException("Failed to extract the iso"); + throw new CloudRuntimeException("Failed to extract the iso"); } } @@ -289,23 +290,23 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe // FIXME: async job needs fixing Long uploadId = extract(caller, templateId, url, zoneId, mode, eventId, false, null, _asyncMgr); if (uploadId != null){ - return uploadId; + return uploadId; }else { - throw new CloudRuntimeException("Failed to extract the teamplate"); + throw new CloudRuntimeException("Failed to extract the teamplate"); } } - + @Override public VirtualMachineTemplate prepareTemplate(long templateId, long zoneId) { - - VMTemplateVO vmTemplate = _tmpltDao.findById(templateId); - if(vmTemplate == null) - throw new InvalidParameterValueException("Unable to find template id=" + templateId); - - _accountMgr.checkAccess(UserContext.current().getCaller(), AccessType.ModifyEntry, true, vmTemplate); - - prepareTemplateInAllStoragePools(vmTemplate, zoneId); - return vmTemplate; + + VMTemplateVO vmTemplate = _tmpltDao.findById(templateId); + if(vmTemplate == null) + throw new InvalidParameterValueException("Unable to find template by id", null); + + _accountMgr.checkAccess(UserContext.current().getCaller(), AccessType.ModifyEntry, true, vmTemplate); + + prepareTemplateInAllStoragePools(vmTemplate, zoneId); + return vmTemplate; } private Long extract(Account caller, Long templateId, String url, Long zoneId, String mode, Long eventId, boolean isISO, AsyncJobVO job, AsyncJobManager mgr) { @@ -314,32 +315,32 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe desc = Upload.Type.ISO.toString(); } eventId = eventId == null ? 0:eventId; - + if (!_accountMgr.isRootAdmin(caller.getType()) && _disableExtraction) { throw new PermissionDeniedException("Extraction has been disabled by admin"); } - + VMTemplateVO template = _tmpltDao.findById(templateId); if (template == null || template.getRemoved() != null) { - throw new InvalidParameterValueException("Unable to find " +desc+ " with id " + templateId); + throw new InvalidParameterValueException("Unable to find " +desc+ " by id", null); } - + if (template.getTemplateType() == Storage.TemplateType.SYSTEM){ - throw new InvalidParameterValueException("Unable to extract the " + desc + " " + template.getName() + " as it is a default System template"); + throw new InvalidParameterValueException("Unable to extract the " + desc + " " + template.getName() + " as it is a default System template", null); } else if (template.getTemplateType() == Storage.TemplateType.PERHOST){ - throw new InvalidParameterValueException("Unable to extract the " + desc + " " + template.getName() + " as it resides on host and not on SSVM"); + throw new InvalidParameterValueException("Unable to extract the " + desc + " " + template.getName() + " as it resides on host and not on SSVM", null); } - + if (isISO) { if (template.getFormat() != ImageFormat.ISO ){ - throw new InvalidParameterValueException("Unsupported format, could not extract the ISO"); + throw new InvalidParameterValueException("Unsupported format, could not extract the ISO", null); } } else { if (template.getFormat() == ImageFormat.ISO ){ - throw new InvalidParameterValueException("Unsupported format, could not extract the template"); + throw new InvalidParameterValueException("Unsupported format, could not extract the template", null); } } - + if (zoneId == null) { zoneId = _swiftMgr.chooseZoneForTmpltExtract(templateId); } @@ -347,13 +348,15 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe if (_dcDao.findById(zoneId) == null) { throw new IllegalArgumentException("Please specify a valid zone."); } - + if (!_accountMgr.isRootAdmin(caller.getType()) && !template.isExtractable()) { - throw new InvalidParameterValueException("Unable to extract template id=" + templateId + " as it's not extractable"); + List idList = new ArrayList(); + idList.add(new IdentityProxy(template, templateId, "templateId")); + throw new InvalidParameterValueException("Unable to extract template id=" + templateId + " as it's not extractable", idList); } - + _accountMgr.checkAccess(caller, AccessType.ModifyEntry, true, template); - + List sservers = _storageMgr.getSecondaryStorageHosts(zoneId); VMTemplateHostVO tmpltHostRef = null; @@ -370,7 +373,7 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe } } } - + if (tmpltHostRef == null && _swiftMgr.isSwiftEnabled()) { SwiftTO swift = _swiftMgr.getSwiftTO(templateId); if (swift != null && sservers != null) { @@ -378,81 +381,82 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe } } if (tmpltHostRef == null) { - throw new InvalidParameterValueException("The " + desc + " has not been downloaded "); + throw new InvalidParameterValueException("The " + desc + " has not been downloaded ", null); } - + Upload.Mode extractMode; if (mode == null || (!mode.equalsIgnoreCase(Upload.Mode.FTP_UPLOAD.toString()) && !mode.equalsIgnoreCase(Upload.Mode.HTTP_DOWNLOAD.toString())) ){ - throw new InvalidParameterValueException("Please specify a valid extract Mode. Supported modes: "+ Upload.Mode.FTP_UPLOAD + ", " + Upload.Mode.HTTP_DOWNLOAD); + throw new InvalidParameterValueException("Please specify a valid extract Mode. Supported modes: "+ Upload.Mode.FTP_UPLOAD + ", " + Upload.Mode.HTTP_DOWNLOAD, null); } else { extractMode = mode.equalsIgnoreCase(Upload.Mode.FTP_UPLOAD.toString()) ? Upload.Mode.FTP_UPLOAD : Upload.Mode.HTTP_DOWNLOAD; } - + if (extractMode == Upload.Mode.FTP_UPLOAD){ URI uri = null; try { uri = new URI(url); if ((uri.getScheme() == null) || (!uri.getScheme().equalsIgnoreCase("ftp") )) { - throw new InvalidParameterValueException("Unsupported scheme for url: " + url); + throw new InvalidParameterValueException("Unsupported scheme for url: " + url, null); } } catch (Exception ex) { - throw new InvalidParameterValueException("Invalid url given: " + url); + throw new InvalidParameterValueException("Invalid url given: " + url, null); } - + String host = uri.getHost(); try { InetAddress hostAddr = InetAddress.getByName(host); if (hostAddr.isAnyLocalAddress() || hostAddr.isLinkLocalAddress() || hostAddr.isLoopbackAddress() || hostAddr.isMulticastAddress() ) { - throw new InvalidParameterValueException("Illegal host specified in url"); + throw new InvalidParameterValueException("Illegal host specified in url", null); } if (hostAddr instanceof Inet6Address) { - throw new InvalidParameterValueException("IPV6 addresses not supported (" + hostAddr.getHostAddress() + ")"); + throw new InvalidParameterValueException("IPV6 addresses not supported (" + hostAddr.getHostAddress() + ")", null); } } catch (UnknownHostException uhe) { - throw new InvalidParameterValueException("Unable to resolve " + host); + throw new InvalidParameterValueException("Unable to resolve " + host, null); } - + if (_uploadMonitor.isTypeUploadInProgress(templateId, isISO ? Type.ISO : Type.TEMPLATE) ){ throw new IllegalArgumentException(template.getName() + " upload is in progress. Please wait for some time to schedule another upload for the same"); } - - return _uploadMonitor.extractTemplate(template, url, tmpltHostRef, zoneId, eventId, job.getId(), mgr); + + return _uploadMonitor.extractTemplate(template, url, tmpltHostRef, zoneId, eventId, job.getId(), mgr); } - + UploadVO vo = _uploadMonitor.createEntityDownloadURL(template, tmpltHostRef, zoneId, eventId); - if (vo != null){ + if (vo != null){ return vo.getId(); }else{ return null; } } - + public void prepareTemplateInAllStoragePools(final VMTemplateVO template, long zoneId) { - List pools = _poolDao.listByStatus(StoragePoolStatus.Up); - for(final StoragePoolVO pool : pools) { - if(pool.getDataCenterId() == zoneId) { - s_logger.info("Schedule to preload template " + template.getId() + " into primary storage " + pool.getId()); - this._preloadExecutor.execute(new Runnable() { - public void run() { - try { - reallyRun(); - } catch(Throwable e) { - s_logger.warn("Unexpected exception ", e); - } - } - - private void reallyRun() { - s_logger.info("Start to preload template " + template.getId() + " into primary storage " + pool.getId()); - prepareTemplateForCreate(template, pool); - s_logger.info("End of preloading template " + template.getId() + " into primary storage " + pool.getId()); - } - }); - } else { - s_logger.info("Skip loading template " + template.getId() + " into primary storage " + pool.getId() + " as pool zone " + pool.getDataCenterId() + " is "); - } - } + List pools = _poolDao.listByStatus(StoragePoolStatus.Up); + for(final StoragePoolVO pool : pools) { + if(pool.getDataCenterId() == zoneId) { + s_logger.info("Schedule to preload template " + template.getId() + " into primary storage " + pool.getId()); + this._preloadExecutor.execute(new Runnable() { + @Override + public void run() { + try { + reallyRun(); + } catch(Throwable e) { + s_logger.warn("Unexpected exception ", e); + } + } + + private void reallyRun() { + s_logger.info("Start to preload template " + template.getId() + " into primary storage " + pool.getId()); + prepareTemplateForCreate(template, pool); + s_logger.info("End of preloading template " + template.getId() + " into primary storage " + pool.getId()); + } + }); + } else { + s_logger.info("Skip loading template " + template.getId() + " into primary storage " + pool.getId() + " as pool zone " + pool.getDataCenterId() + " is "); + } + } } - + String downloadTemplateFromSwiftToSecondaryStorage(long dcId, long templateId){ VMTemplateVO template = _tmpltDao.findById(templateId); if ( template == null ) { @@ -558,8 +562,8 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe @Override @DB public VMTemplateStoragePoolVO prepareTemplateForCreate(VMTemplateVO template, StoragePool pool) { - template = _tmpltDao.findById(template.getId(), true); - + template = _tmpltDao.findById(template.getId(), true); + long poolId = pool.getId(); long templateId = template.getId(); long dcId = pool.getDataCenterId(); @@ -567,23 +571,23 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe VMTemplateHostVO templateHostRef = null; long templateStoragePoolRefId; String origUrl = null; - + templateStoragePoolRef = _tmpltPoolDao.findByPoolTemplate(poolId, templateId); if (templateStoragePoolRef != null) { - templateStoragePoolRef.setMarkedForGC(false); + templateStoragePoolRef.setMarkedForGC(false); _tmpltPoolDao.update(templateStoragePoolRef.getId(), templateStoragePoolRef); - + if (templateStoragePoolRef.getDownloadState() == Status.DOWNLOADED) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Template " + templateId + " has already been downloaded to pool " + poolId); - } - - return templateStoragePoolRef; - } + if (s_logger.isDebugEnabled()) { + s_logger.debug("Template " + templateId + " has already been downloaded to pool " + poolId); + } + + return templateStoragePoolRef; + } } - + templateHostRef = _storageMgr.findVmTemplateHost(templateId, pool); - + if (templateHostRef == null || templateHostRef.getDownloadState() != Status.DOWNLOADED) { String result = downloadTemplateFromSwiftToSecondaryStorage(dcId, templateId); if (result != null) { @@ -596,13 +600,13 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe return null; } } - + HostVO sh = _hostDao.findById(templateHostRef.getHostId()); origUrl = sh.getStorageUrl(); if (origUrl == null) { throw new CloudRuntimeException("Unable to find the orig.url from host " + sh.toString()); } - + if (templateStoragePoolRef == null) { if (s_logger.isDebugEnabled()) { s_logger.debug("Downloading template " + templateId + " to pool " + poolId); @@ -611,7 +615,7 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe try { templateStoragePoolRef = _tmpltPoolDao.persist(templateStoragePoolRef); templateStoragePoolRefId = templateStoragePoolRef.getId(); - + } catch (Exception e) { s_logger.debug("Assuming we're in a race condition: " + e.getMessage()); templateStoragePoolRef = _tmpltPoolDao.findByPoolTemplate(poolId, templateId); @@ -623,12 +627,12 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe } else { templateStoragePoolRefId = templateStoragePoolRef.getId(); } - + List vos = _poolHostDao.listByHostStatus(poolId, com.cloud.host.Status.Up); if (vos == null || vos.isEmpty()){ - throw new CloudRuntimeException("Cannot download " + templateId + " to poolId " + poolId + " since there is no host in the Up state connected to this pool"); + throw new CloudRuntimeException("Cannot download " + templateId + " to poolId " + poolId + " since there is no host in the Up state connected to this pool"); } - + templateStoragePoolRef = _tmpltPoolDao.acquireInLockTable(templateStoragePoolRefId, _storagePoolMaxWaitSeconds); if (templateStoragePoolRef == null) { throw new CloudRuntimeException("Unable to acquire lock on VMTemplateStoragePool: " + templateStoragePoolRefId); @@ -640,39 +644,39 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe } String url = origUrl + "/" + templateHostRef.getInstallPath(); PrimaryStorageDownloadCommand dcmd = new PrimaryStorageDownloadCommand(template.getUniqueName(), url, template.getFormat(), - template.getAccountId(), pool.getId(), pool.getUuid(), _primaryStorageDownloadWait); + template.getAccountId(), pool.getId(), pool.getUuid(), _primaryStorageDownloadWait); HostVO secondaryStorageHost = _hostDao.findById(templateHostRef.getHostId()); assert(secondaryStorageHost != null); dcmd.setSecondaryStorageUrl(secondaryStorageHost.getStorageUrl()); // TODO temporary hacking, hard-coded to NFS primary data store dcmd.setPrimaryStorageUrl("nfs://" + pool.getHostAddress() + pool.getPath()); - + for (int retry = 0; retry < 2; retry ++){ - Collections.shuffle(vos); // Shuffling to pick a random host in the vm deployment retries - StoragePoolHostVO vo = vos.get(0); - if (s_logger.isDebugEnabled()) { - s_logger.debug("Downloading " + templateId + " via " + vo.getHostId()); - } - dcmd.setLocalPath(vo.getLocalPath()); - // set 120 min timeout for this command - - PrimaryStorageDownloadAnswer answer = (PrimaryStorageDownloadAnswer)_agentMgr.easySend( - _hvGuruMgr.getGuruProcessedCommandTargetHost(vo.getHostId(), dcmd), dcmd); - if (answer != null && answer.getResult() ) { - templateStoragePoolRef.setDownloadPercent(100); - templateStoragePoolRef.setDownloadState(Status.DOWNLOADED); - templateStoragePoolRef.setLocalDownloadPath(answer.getInstallPath()); - templateStoragePoolRef.setInstallPath(answer.getInstallPath()); - templateStoragePoolRef.setTemplateSize(answer.getTemplateSize()); - _tmpltPoolDao.update(templateStoragePoolRef.getId(), templateStoragePoolRef); - if (s_logger.isDebugEnabled()) { - s_logger.debug("Template " + templateId + " is downloaded via " + vo.getHostId()); - } - return templateStoragePoolRef; - } else { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Template " + templateId + " download to pool " + vo.getPoolId() + " failed due to " + (answer!=null?answer.getDetails():"return null")); } - } + Collections.shuffle(vos); // Shuffling to pick a random host in the vm deployment retries + StoragePoolHostVO vo = vos.get(0); + if (s_logger.isDebugEnabled()) { + s_logger.debug("Downloading " + templateId + " via " + vo.getHostId()); + } + dcmd.setLocalPath(vo.getLocalPath()); + // set 120 min timeout for this command + + PrimaryStorageDownloadAnswer answer = (PrimaryStorageDownloadAnswer)_agentMgr.easySend( + _hvGuruMgr.getGuruProcessedCommandTargetHost(vo.getHostId(), dcmd), dcmd); + if (answer != null && answer.getResult() ) { + templateStoragePoolRef.setDownloadPercent(100); + templateStoragePoolRef.setDownloadState(Status.DOWNLOADED); + templateStoragePoolRef.setLocalDownloadPath(answer.getInstallPath()); + templateStoragePoolRef.setInstallPath(answer.getInstallPath()); + templateStoragePoolRef.setTemplateSize(answer.getTemplateSize()); + _tmpltPoolDao.update(templateStoragePoolRef.getId(), templateStoragePoolRef); + if (s_logger.isDebugEnabled()) { + s_logger.debug("Template " + templateId + " is downloaded via " + vo.getHostId()); + } + return templateStoragePoolRef; + } else { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Template " + templateId + " download to pool " + vo.getPoolId() + " failed due to " + (answer!=null?answer.getDetails():"return null")); } + } } } finally { _tmpltPoolDao.releaseFromLockTable(templateStoragePoolRefId); @@ -682,7 +686,7 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe } return null; } - + @Override @DB public VMTemplateHostVO prepareISOForCreate(VMTemplateVO template, StoragePool pool) { @@ -716,36 +720,36 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe @Override @DB public boolean resetTemplateDownloadStateOnPool(long templateStoragePoolRefId) { - // have to use the same lock that prepareTemplateForCreate use to maintain state consistency - VMTemplateStoragePoolVO templateStoragePoolRef = _tmpltPoolDao.acquireInLockTable(templateStoragePoolRefId, 1200); - + // have to use the same lock that prepareTemplateForCreate use to maintain state consistency + VMTemplateStoragePoolVO templateStoragePoolRef = _tmpltPoolDao.acquireInLockTable(templateStoragePoolRefId, 1200); + if (templateStoragePoolRef == null) { - s_logger.warn("resetTemplateDownloadStateOnPool failed - unable to lock TemplateStorgePoolRef " + templateStoragePoolRefId); + s_logger.warn("resetTemplateDownloadStateOnPool failed - unable to lock TemplateStorgePoolRef " + templateStoragePoolRefId); return false; } - + try { - templateStoragePoolRef.setDownloadState(VMTemplateStorageResourceAssoc.Status.NOT_DOWNLOADED); - _tmpltPoolDao.update(templateStoragePoolRefId, templateStoragePoolRef); + templateStoragePoolRef.setDownloadState(VMTemplateStorageResourceAssoc.Status.NOT_DOWNLOADED); + _tmpltPoolDao.update(templateStoragePoolRefId, templateStoragePoolRef); } finally { _tmpltPoolDao.releaseFromLockTable(templateStoragePoolRefId); } - + return true; } - + @Override @DB public boolean copy(long userId, VMTemplateVO template, HostVO srcSecHost, DataCenterVO srcZone, DataCenterVO dstZone) throws StorageUnavailableException, ResourceAllocationException { - List dstSecHosts = _ssvmMgr.listSecondaryStorageHostsInOneZone(dstZone.getId()); - long tmpltId = template.getId(); + List dstSecHosts = _ssvmMgr.listSecondaryStorageHostsInOneZone(dstZone.getId()); + long tmpltId = template.getId(); long dstZoneId = dstZone.getId(); - if (dstSecHosts == null || dstSecHosts.isEmpty() ) { - throw new StorageUnavailableException("Destination zone is not ready", DataCenter.class, dstZone.getId()); - } + if (dstSecHosts == null || dstSecHosts.isEmpty() ) { + throw new StorageUnavailableException("Destination zone is not ready", DataCenter.class, dstZone.getId()); + } AccountVO account = _accountDao.findById(template.getAccountId()); _resourceLimitMgr.checkResourceLimit(account, ResourceType.template); - + // Event details String copyEventType; String createEventType; @@ -756,162 +760,164 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe copyEventType = EventTypes.EVENT_TEMPLATE_COPY; createEventType = EventTypes.EVENT_TEMPLATE_CREATE; } - - + + Transaction txn = Transaction.currentTxn(); txn.start(); - + VMTemplateHostVO srcTmpltHost = _tmpltHostDao.findByHostTemplate(srcSecHost.getId(), tmpltId); for ( HostVO dstSecHost : dstSecHosts ) { VMTemplateHostVO dstTmpltHost = null; try { - dstTmpltHost = _tmpltHostDao.findByHostTemplate(dstSecHost.getId(), tmpltId, true); - if (dstTmpltHost != null) { - dstTmpltHost = _tmpltHostDao.lockRow(dstTmpltHost.getId(), true); - if (dstTmpltHost != null && dstTmpltHost.getDownloadState() == Status.DOWNLOADED) { - if (dstTmpltHost.getDestroyed() == false) { - return true; - } else { - dstTmpltHost.setDestroyed(false); - _tmpltHostDao.update(dstTmpltHost.getId(), dstTmpltHost); - - return true; - } - } else if (dstTmpltHost != null && dstTmpltHost.getDownloadState() == Status.DOWNLOAD_ERROR){ - if (dstTmpltHost.getDestroyed() == true) { - dstTmpltHost.setDestroyed(false); - dstTmpltHost.setDownloadState(Status.NOT_DOWNLOADED); - dstTmpltHost.setDownloadPercent(0); - dstTmpltHost.setCopy(true); - dstTmpltHost.setErrorString(""); - dstTmpltHost.setJobId(null); - _tmpltHostDao.update(dstTmpltHost.getId(), dstTmpltHost); - } - } - } + dstTmpltHost = _tmpltHostDao.findByHostTemplate(dstSecHost.getId(), tmpltId, true); + if (dstTmpltHost != null) { + dstTmpltHost = _tmpltHostDao.lockRow(dstTmpltHost.getId(), true); + if (dstTmpltHost != null && dstTmpltHost.getDownloadState() == Status.DOWNLOADED) { + if (dstTmpltHost.getDestroyed() == false) { + return true; + } else { + dstTmpltHost.setDestroyed(false); + _tmpltHostDao.update(dstTmpltHost.getId(), dstTmpltHost); + + return true; + } + } else if (dstTmpltHost != null && dstTmpltHost.getDownloadState() == Status.DOWNLOAD_ERROR){ + if (dstTmpltHost.getDestroyed() == true) { + dstTmpltHost.setDestroyed(false); + dstTmpltHost.setDownloadState(Status.NOT_DOWNLOADED); + dstTmpltHost.setDownloadPercent(0); + dstTmpltHost.setCopy(true); + dstTmpltHost.setErrorString(""); + dstTmpltHost.setJobId(null); + _tmpltHostDao.update(dstTmpltHost.getId(), dstTmpltHost); + } + } + } } finally { - txn.commit(); + txn.commit(); } - + if(_downloadMonitor.copyTemplate(template, srcSecHost, dstSecHost) ) { _tmpltDao.addTemplateToZone(template, dstZoneId); - - if(account.getId() != Account.ACCOUNT_ID_SYSTEM){ - UsageEventVO usageEvent = new UsageEventVO(copyEventType, account.getId(), dstZoneId, tmpltId, null, null, null, srcTmpltHost.getSize()); - _usageEventDao.persist(usageEvent); - } - return true; + + if(account.getId() != Account.ACCOUNT_ID_SYSTEM){ + UsageEventVO usageEvent = new UsageEventVO(copyEventType, account.getId(), dstZoneId, tmpltId, null, null, null, srcTmpltHost.getSize()); + _usageEventDao.persist(usageEvent); + } + return true; } } return false; } - - + + @Override @ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_COPY, eventDescription = "copying template", async = true) public VirtualMachineTemplate copyTemplate(CopyTemplateCmd cmd) throws StorageUnavailableException, ResourceAllocationException { - Long templateId = cmd.getId(); - Long userId = UserContext.current().getCallerUserId(); - Long sourceZoneId = cmd.getSourceZoneId(); - Long destZoneId = cmd.getDestinationZoneId(); - Account caller = UserContext.current().getCaller(); - + Long templateId = cmd.getId(); + Long userId = UserContext.current().getCallerUserId(); + Long sourceZoneId = cmd.getSourceZoneId(); + Long destZoneId = cmd.getDestinationZoneId(); + Account caller = UserContext.current().getCaller(); + if (_swiftMgr.isSwiftEnabled()) { throw new CloudRuntimeException("copytemplate API is disabled in Swift setup, templates in Swift can be accessed by all Zones"); } //Verify parameters if (sourceZoneId == destZoneId) { - throw new InvalidParameterValueException("Please specify different source and destination zones."); + throw new InvalidParameterValueException("Please specify different source and destination zones.", null); } - + DataCenterVO sourceZone = _dcDao.findById(sourceZoneId); if (sourceZone == null) { - throw new InvalidParameterValueException("Please specify a valid source zone."); + throw new InvalidParameterValueException("Please specify a valid source zone.", null); } - + DataCenterVO dstZone = _dcDao.findById(destZoneId); if (dstZone == null) { - throw new InvalidParameterValueException("Please specify a valid destination zone."); + throw new InvalidParameterValueException("Please specify a valid destination zone.", null); } - + VMTemplateVO template = _tmpltDao.findById(templateId); if (template == null || template.getRemoved() != null) { - throw new InvalidParameterValueException("Unable to find template with id"); + throw new InvalidParameterValueException("Unable to find template by id", null); } - + HostVO dstSecHost = _storageMgr.getSecondaryStorageHost(destZoneId, templateId); if ( dstSecHost != null ) { s_logger.debug("There is template " + templateId + " in secondary storage " + dstSecHost.getId() + " in zone " + destZoneId + " , don't need to copy"); return template; } - + HostVO srcSecHost = _storageMgr.getSecondaryStorageHost(sourceZoneId, templateId); if ( srcSecHost == null ) { - throw new InvalidParameterValueException("There is no template " + templateId + " in zone " + sourceZoneId ); + List idList = new ArrayList(); + idList.add(new IdentityProxy(sourceZone, sourceZoneId, "zoneId")); + throw new InvalidParameterValueException("Couldn't find template by id in source zone with specified zoneId", idList); } - + _accountMgr.checkAccess(caller, AccessType.ModifyEntry, true, template); - + boolean success = copy(userId, template, srcSecHost, sourceZone, dstZone); - - if (success){ - return template; + + if (success){ + return template; }else { - throw new CloudRuntimeException("Failed to copy template"); + throw new CloudRuntimeException("Failed to copy template"); } } @Override public boolean delete(long userId, long templateId, Long zoneId) { - VMTemplateVO template = _tmpltDao.findById(templateId); - if (template == null || template.getRemoved() != null) { - throw new InvalidParameterValueException("Please specify a valid template."); - } - - TemplateAdapter adapter = getAdapter(template.getHypervisorType()); - return adapter.delete(new TemplateProfile(userId, template, zoneId)); + VMTemplateVO template = _tmpltDao.findById(templateId); + if (template == null || template.getRemoved() != null) { + throw new InvalidParameterValueException("Please specify a valid template.", null); + } + + TemplateAdapter adapter = getAdapter(template.getHypervisorType()); + return adapter.delete(new TemplateProfile(userId, template, zoneId)); } - + @Override public List getUnusedTemplatesInPool(StoragePoolVO pool) { - List unusedTemplatesInPool = new ArrayList(); - List allTemplatesInPool = _tmpltPoolDao.listByPoolId(pool.getId()); - - for (VMTemplateStoragePoolVO templatePoolVO : allTemplatesInPool) { - VMTemplateVO template = _tmpltDao.findByIdIncludingRemoved(templatePoolVO.getTemplateId()); - - // If this is a routing template, consider it in use - if (template.getTemplateType() == TemplateType.SYSTEM) { - continue; - } - - // If the template is not yet downloaded to the pool, consider it in use - if (templatePoolVO.getDownloadState() != Status.DOWNLOADED) { - continue; - } + List unusedTemplatesInPool = new ArrayList(); + List allTemplatesInPool = _tmpltPoolDao.listByPoolId(pool.getId()); - if (template.getFormat() != ImageFormat.ISO && !_volumeDao.isAnyVolumeActivelyUsingTemplateOnPool(template.getId(), pool.getId())) { + for (VMTemplateStoragePoolVO templatePoolVO : allTemplatesInPool) { + VMTemplateVO template = _tmpltDao.findByIdIncludingRemoved(templatePoolVO.getTemplateId()); + + // If this is a routing template, consider it in use + if (template.getTemplateType() == TemplateType.SYSTEM) { + continue; + } + + // If the template is not yet downloaded to the pool, consider it in use + if (templatePoolVO.getDownloadState() != Status.DOWNLOADED) { + continue; + } + + if (template.getFormat() != ImageFormat.ISO && !_volumeDao.isAnyVolumeActivelyUsingTemplateOnPool(template.getId(), pool.getId())) { unusedTemplatesInPool.add(templatePoolVO); - } - } - - return unusedTemplatesInPool; - } - + } + } + + return unusedTemplatesInPool; + } + @Override public void evictTemplateFromStoragePool(VMTemplateStoragePoolVO templatePoolVO) { - StoragePoolVO pool = _poolDao.findById(templatePoolVO.getPoolId()); - VMTemplateVO template = _tmpltDao.findByIdIncludingRemoved(templatePoolVO.getTemplateId()); - - - if (s_logger.isDebugEnabled()) { - s_logger.debug("Evicting " + templatePoolVO); - } - DestroyCommand cmd = new DestroyCommand(pool, templatePoolVO); - + StoragePoolVO pool = _poolDao.findById(templatePoolVO.getPoolId()); + VMTemplateVO template = _tmpltDao.findByIdIncludingRemoved(templatePoolVO.getTemplateId()); + + + if (s_logger.isDebugEnabled()) { + s_logger.debug("Evicting " + templatePoolVO); + } + DestroyCommand cmd = new DestroyCommand(pool, templatePoolVO); + try { Answer answer = _storageMgr.sendToPool(pool, cmd); - + if (answer != null && answer.getResult()) { // Remove the templatePoolVO if (_tmpltPoolDao.remove(templatePoolVO.getId())) { @@ -924,8 +930,8 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe s_logger.info("Storage is unavailable currently. Will retry evicte template: " + template.getName() + " from storage pool: " + pool.getName()); } - } - + } + void swiftTemplateSync() { GlobalLock swiftTemplateSyncLock = GlobalLock.getInternLock("templatemgr.swiftTemplateSync"); try { @@ -942,7 +948,7 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe } List templtSwiftRefs = _tmpltSwiftDao.listAll(); for (VMTemplateSwiftVO templtSwiftRef : templtSwiftRefs) { - templateIds.remove((Long) templtSwiftRef.getTemplateId()); + templateIds.remove(templtSwiftRef.getTemplateId()); } if (templateIds.size() < 1) { return; @@ -1011,9 +1017,9 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe @Override public boolean configure(String name, Map params) throws ConfigurationException { _name = name; - + ComponentLocator locator = ComponentLocator.getCurrentLocator(); - + final Map configs = _configDao.getConfiguration("AgentManager", params); _routerTemplateId = NumbersUtil.parseInt(configs.get("router.template.id"), 1); @@ -1026,143 +1032,143 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe HostTemplateStatesSearch = _tmpltHostDao.createSearchBuilder(); HostTemplateStatesSearch.and("id", HostTemplateStatesSearch.entity().getTemplateId(), SearchCriteria.Op.EQ); HostTemplateStatesSearch.and("state", HostTemplateStatesSearch.entity().getDownloadState(), SearchCriteria.Op.EQ); - + SearchBuilder HostSearch = _hostDao.createSearchBuilder(); HostSearch.and("dcId", HostSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ); - + HostTemplateStatesSearch.join("host", HostSearch, HostSearch.entity().getId(), HostTemplateStatesSearch.entity().getHostId(), JoinBuilder.JoinType.INNER); HostSearch.done(); HostTemplateStatesSearch.done(); - + _storagePoolMaxWaitSeconds = NumbersUtil.parseInt(_configDao.getValue(Config.StoragePoolMaxWaitSeconds.key()), 3600); _preloadExecutor = Executors.newFixedThreadPool(8, new NamedThreadFactory("Template-Preloader")); _swiftTemplateSyncExecutor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("swift-template-sync-Executor")); return false; } - + protected TemplateManagerImpl() { } - @Override + @Override public boolean templateIsDeleteable(VMTemplateHostVO templateHostRef) { - VMTemplateVO template = _tmpltDao.findByIdIncludingRemoved(templateHostRef.getTemplateId()); - long templateId = template.getId(); - HostVO secondaryStorageHost = _hostDao.findById(templateHostRef.getHostId()); - long zoneId = secondaryStorageHost.getDataCenterId(); - DataCenterVO zone = _dcDao.findById(zoneId); - - // Check if there are VMs running in the template host ref's zone that use the template - List nonExpungedVms = _vmInstanceDao.listNonExpungedByZoneAndTemplate(zoneId, templateId); - - if (!nonExpungedVms.isEmpty()) { - s_logger.debug("Template " + template.getName() + " in zone " + zone.getName() + " is not deleteable because there are non-expunged VMs deployed from this template."); - return false; - } - - // Check if there are any snapshots for the template in the template host ref's zone - List volumes = _volumeDao.findByTemplateAndZone(templateId, zoneId); - for (VolumeVO volume : volumes) { - List snapshots = _snapshotDao.listByVolumeIdVersion(volume.getId(), "2.1"); - if (!snapshots.isEmpty()) { - s_logger.debug("Template " + template.getName() + " in zone " + zone.getName() + " is not deleteable because there are 2.1 snapshots using this template."); - return false; - } - } - - return true; - } + VMTemplateVO template = _tmpltDao.findByIdIncludingRemoved(templateHostRef.getTemplateId()); + long templateId = template.getId(); + HostVO secondaryStorageHost = _hostDao.findById(templateHostRef.getHostId()); + long zoneId = secondaryStorageHost.getDataCenterId(); + DataCenterVO zone = _dcDao.findById(zoneId); - @Override + // Check if there are VMs running in the template host ref's zone that use the template + List nonExpungedVms = _vmInstanceDao.listNonExpungedByZoneAndTemplate(zoneId, templateId); + + if (!nonExpungedVms.isEmpty()) { + s_logger.debug("Template " + template.getName() + " in zone " + zone.getName() + " is not deleteable because there are non-expunged VMs deployed from this template."); + return false; + } + + // Check if there are any snapshots for the template in the template host ref's zone + List volumes = _volumeDao.findByTemplateAndZone(templateId, zoneId); + for (VolumeVO volume : volumes) { + List snapshots = _snapshotDao.listByVolumeIdVersion(volume.getId(), "2.1"); + if (!snapshots.isEmpty()) { + s_logger.debug("Template " + template.getName() + " in zone " + zone.getName() + " is not deleteable because there are 2.1 snapshots using this template."); + return false; + } + } + + return true; + } + + @Override @ActionEvent(eventType = EventTypes.EVENT_ISO_DETACH, eventDescription = "detaching ISO", async = true) - public boolean detachIso(long vmId) { + public boolean detachIso(long vmId) { Account caller = UserContext.current().getCaller(); Long userId = UserContext.current().getCallerUserId(); - + // Verify input parameters UserVmVO vmInstanceCheck = _userVmDao.findById(vmId); if (vmInstanceCheck == null) { - throw new InvalidParameterValueException ("Unable to find a virtual machine with id " + vmId); + throw new InvalidParameterValueException ("Unable to find a virtual machine by id", null); } - + UserVm userVM = _userVmDao.findById(vmId); if (userVM == null) { - throw new InvalidParameterValueException("Please specify a valid VM."); + throw new InvalidParameterValueException("Please specify a valid VM.", null); } - + _accountMgr.checkAccess(caller, null, true, userVM); Long isoId = userVM.getIsoId(); if (isoId == null) { - throw new InvalidParameterValueException("The specified VM has no ISO attached to it."); + throw new InvalidParameterValueException("The specified VM has no ISO attached to it.", null); } - UserContext.current().setEventDetails("Vm Id: " +vmId+ " ISO Id: "+isoId); - + UserContext.current().setEventDetails("Vm Id: " +vmId+ " ISO Id: "+isoId); + State vmState = userVM.getState(); if (vmState != State.Running && vmState != State.Stopped) { - throw new InvalidParameterValueException("Please specify a VM that is either Stopped or Running."); + throw new InvalidParameterValueException("Please specify a VM that is either Stopped or Running.", null); } boolean result = attachISOToVM(vmId, userId, isoId, false); //attach=false => detach if (result){ - return result; + return result; }else { - throw new CloudRuntimeException("Failed to detach iso"); + throw new CloudRuntimeException("Failed to detach iso"); } - } - - @Override + } + + @Override @ActionEvent(eventType = EventTypes.EVENT_ISO_ATTACH, eventDescription = "attaching ISO", async = true) - public boolean attachIso(long isoId, long vmId) { + public boolean attachIso(long isoId, long vmId) { Account caller = UserContext.current().getCaller(); Long userId = UserContext.current().getCallerUserId(); - - // Verify input parameters - UserVmVO vm = _userVmDao.findById(vmId); - if (vm == null) { - throw new InvalidParameterValueException("Unable to find a virtual machine with id " + vmId); + + // Verify input parameters + UserVmVO vm = _userVmDao.findById(vmId); + if (vm == null) { + throw new InvalidParameterValueException("Unable to find a virtual machine by id ", null); } - - VMTemplateVO iso = _tmpltDao.findById(isoId); - if (iso == null || iso.getRemoved() != null) { - throw new InvalidParameterValueException("Unable to find an ISO with id " + isoId); - } - - //check permissions - //check if caller has access to VM and ISO - //and also check if the VM's owner has access to the ISO. - - _accountMgr.checkAccess(caller, null, false, iso, vm); - - Account vmOwner = _accountDao.findById(vm.getAccountId()); - _accountMgr.checkAccess(vmOwner, null, false, iso, vm); - + + VMTemplateVO iso = _tmpltDao.findById(isoId); + if (iso == null || iso.getRemoved() != null) { + throw new InvalidParameterValueException("Unable to find an ISO by id", null); + } + + //check permissions + //check if caller has access to VM and ISO + //and also check if the VM's owner has access to the ISO. + + _accountMgr.checkAccess(caller, null, false, iso, vm); + + Account vmOwner = _accountDao.findById(vm.getAccountId()); + _accountMgr.checkAccess(vmOwner, null, false, iso, vm); + State vmState = vm.getState(); if (vmState != State.Running && vmState != State.Stopped) { - throw new InvalidParameterValueException("Please specify a VM that is either Stopped or Running."); + throw new InvalidParameterValueException("Please specify a VM that is either Stopped or Running.", null); } - + if ("xen-pv-drv-iso".equals(iso.getDisplayText()) && vm.getHypervisorType() != Hypervisor.HypervisorType.XenServer){ - throw new InvalidParameterValueException("Cannot attach Xenserver PV drivers to incompatible hypervisor " + vm.getHypervisorType()); + throw new InvalidParameterValueException("Cannot attach Xenserver PV drivers to incompatible hypervisor " + vm.getHypervisorType(), null); } - + if("vmware-tools.iso".equals(iso.getName()) && vm.getHypervisorType() != Hypervisor.HypervisorType.VMware) { - throw new InvalidParameterValueException("Cannot attach VMware tools drivers to incompatible hypervisor " + vm.getHypervisorType()); + throw new InvalidParameterValueException("Cannot attach VMware tools drivers to incompatible hypervisor " + vm.getHypervisorType(), null); } boolean result = attachISOToVM(vmId, userId, isoId, true); if (result){ - return result; + return result; }else { - throw new CloudRuntimeException("Failed to attach iso"); + throw new CloudRuntimeException("Failed to attach iso"); } - } + } private boolean attachISOToVM(long vmId, long userId, long isoId, boolean attach) { - UserVmVO vm = _userVmDao.findById(vmId); - VMTemplateVO iso = _tmpltDao.findById(isoId); + UserVmVO vm = _userVmDao.findById(vmId); + VMTemplateVO iso = _tmpltDao.findById(isoId); boolean success = _vmMgr.attachISOToVM(vmId, isoId, attach); if ( success && attach) { - vm.setIsoId(iso.getId()); + vm.setIsoId(iso.getId()); _userVmDao.update(vmId, vm); } if ( success && !attach ) { @@ -1171,31 +1177,31 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe } return success; } - - @Override + + @Override @ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_DELETE, eventDescription = "deleting template", async = true) public boolean deleteTemplate(DeleteTemplateCmd cmd) { Long templateId = cmd.getId(); Account caller = UserContext.current().getCaller(); - + VirtualMachineTemplate template = getTemplate(templateId); if (template == null) { - throw new InvalidParameterValueException("unable to find template with id " + templateId); + throw new InvalidParameterValueException("unable to find template by id", null); } - + _accountMgr.checkAccess(caller, AccessType.ModifyEntry, true, template); - - if (template.getFormat() == ImageFormat.ISO) { - throw new InvalidParameterValueException("Please specify a valid template."); - } + + if (template.getFormat() == ImageFormat.ISO) { + throw new InvalidParameterValueException("Please specify a valid template.", null); + } if (cmd.getZoneId() == null && _swiftMgr.isSwiftEnabled()) { _swiftMgr.deleteTemplate(cmd); } - TemplateAdapter adapter = getAdapter(template.getHypervisorType()); - TemplateProfile profile = adapter.prepareDelete(cmd); - boolean result = adapter.delete(profile); - - if (result){ + TemplateAdapter adapter = getAdapter(template.getHypervisorType()); + TemplateProfile profile = adapter.prepareDelete(cmd); + boolean result = adapter.delete(profile); + + if (result){ if (cmd.getZoneId() == null && _swiftMgr.isSwiftEnabled()) { List templateZones = _tmpltZoneDao.listByZoneTemplate(null, templateId); if (templateZones != null) { @@ -1204,37 +1210,37 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe } } } - return true; - }else{ - throw new CloudRuntimeException("Failed to delete template"); - } - } - - @Override + return true; + }else{ + throw new CloudRuntimeException("Failed to delete template"); + } + } + + @Override @ActionEvent(eventType = EventTypes.EVENT_ISO_DELETE, eventDescription = "deleting iso", async = true) public boolean deleteIso(DeleteIsoCmd cmd) { Long templateId = cmd.getId(); Account caller = UserContext.current().getCaller(); Long zoneId = cmd.getZoneId(); - + VirtualMachineTemplate template = getTemplate(templateId);; if (template == null) { - throw new InvalidParameterValueException("unable to find iso with id " + templateId); + throw new InvalidParameterValueException("unable to find iso by id", null); } - + _accountMgr.checkAccess(caller, AccessType.ModifyEntry, true, template); - - if (template.getFormat() != ImageFormat.ISO) { - throw new InvalidParameterValueException("Please specify a valid iso."); - } + + if (template.getFormat() != ImageFormat.ISO) { + throw new InvalidParameterValueException("Please specify a valid iso.", null); + } if (cmd.getZoneId() == null && _swiftMgr.isSwiftEnabled()) { _swiftMgr.deleteIso(cmd); - } - if (zoneId != null && (_ssvmMgr.findSecondaryStorageHost(zoneId) == null)) { - throw new InvalidParameterValueException("Failed to find a secondary storage host in the specified zone."); - } - TemplateAdapter adapter = getAdapter(template.getHypervisorType()); - TemplateProfile profile = adapter.prepareDelete(cmd); + } + if (zoneId != null && (_ssvmMgr.findSecondaryStorageHost(zoneId) == null)) { + throw new InvalidParameterValueException("Failed to find a secondary storage host in the specified zone.", null); + } + TemplateAdapter adapter = getAdapter(template.getHypervisorType()); + TemplateProfile profile = adapter.prepareDelete(cmd); boolean result = adapter.delete(profile); if (result) { if (cmd.getZoneId() == null && _swiftMgr.isSwiftEnabled()) { @@ -1247,20 +1253,20 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe } return true; } else { - throw new CloudRuntimeException("Failed to delete ISO"); - } - } - - @Override - public VirtualMachineTemplate getTemplate(long templateId) { - VMTemplateVO template = _tmpltDao.findById(templateId); - if (template != null && template.getRemoved() == null) { - return template; - } - - return null; - } - + throw new CloudRuntimeException("Failed to delete ISO"); + } + } + + @Override + public VirtualMachineTemplate getTemplate(long templateId) { + VMTemplateVO template = _tmpltDao.findById(templateId); + if (template != null && template.getRemoved() == null) { + return template; + } + + return null; + } + @Override public List listTemplatePermissions(ListTemplateOrIsoPermissionsCmd cmd) { Account caller = UserContext.current().getCaller(); @@ -1272,16 +1278,16 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe VirtualMachineTemplate template = getTemplate(id); if (template == null) { - throw new InvalidParameterValueException("unable to find " + cmd.getMediaType() + " with id " + id); + throw new InvalidParameterValueException("unable to find " + cmd.getMediaType() + " by template id", null); } - + if (cmd instanceof ListTemplatePermissionsCmd) { if (template.getFormat().equals(ImageFormat.ISO)) { - throw new InvalidParameterValueException("Please provide a valid template"); + throw new InvalidParameterValueException("Please provide a valid template", null); } } else if (cmd instanceof ListIsoPermissionsCmd) { if (!template.getFormat().equals(ImageFormat.ISO)) { - throw new InvalidParameterValueException("Please provide a valid iso"); + throw new InvalidParameterValueException("Please provide a valid iso", null); } } @@ -1304,7 +1310,7 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe } return accountNames; } - + @DB @Override public boolean updateTemplateOrIsoPermissions(UpdateTemplateOrIsoPermissionsCmd cmd) { @@ -1324,32 +1330,34 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe VMTemplateVO template = _tmpltDao.findById(id); if (template == null) { - throw new InvalidParameterValueException("unable to find " + mediaType + " with id " + id); + throw new InvalidParameterValueException("unable to find " + mediaType + " by template id", null); } if (cmd instanceof UpdateTemplatePermissionsCmd) { mediaType = "template"; if (template.getFormat().equals(ImageFormat.ISO)) { - throw new InvalidParameterValueException("Please provide a valid template"); + throw new InvalidParameterValueException("Please provide a valid template", null); } } if (cmd instanceof UpdateIsoPermissionsCmd) { mediaType = "iso"; if (!template.getFormat().equals(ImageFormat.ISO)) { - throw new InvalidParameterValueException("Please provide a valid iso"); + throw new InvalidParameterValueException("Please provide a valid iso", null); } } - + //convert projectIds to accountNames if (projectIds != null) { for (Long projectId : projectIds) { Project project = _projectMgr.getProject(projectId); if (project == null) { - throw new InvalidParameterValueException("Unable to find project by id " + projectId); + throw new InvalidParameterValueException("Unable to find project by id ", null); } - + if (!_projectMgr.canAccessProjectAccount(caller, project.getProjectAccountId())) { - throw new InvalidParameterValueException("Account " + caller + " can't access project id=" + projectId); + List idList = new ArrayList(); + idList.add(new IdentityProxy(project, projectId, "projectId")); + throw new InvalidParameterValueException("Account " + caller + " can't access project with specified projectId", idList); } accountNames.add(_accountMgr.getAccount(project.getProjectAccountId()).getAccountName()); } @@ -1360,23 +1368,23 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe // If the template is removed throw an error. if (template.getRemoved() != null) { s_logger.error("unable to update permissions for " + mediaType + " with id " + id + " as it is removed "); - throw new InvalidParameterValueException("unable to update permissions for " + mediaType + " with id " + id + " as it is removed "); + throw new InvalidParameterValueException("unable to update permissions for " + mediaType + " with id " + id + " as it is removed ", null); } if (id == Long.valueOf(1)) { - throw new InvalidParameterValueException("unable to update permissions for " + mediaType + " with id " + id); + throw new InvalidParameterValueException("unable to update permissions for " + mediaType + " with id " + id, null); } boolean isAdmin = _accountMgr.isAdmin(caller.getType()); boolean allowPublicUserTemplates = Boolean.valueOf(_configDao.getValue("allow.public.user.templates")); if (!isAdmin && !allowPublicUserTemplates && isPublic != null && isPublic) { - throw new InvalidParameterValueException("Only private " + mediaType + "s can be created."); + throw new InvalidParameterValueException("Only private " + mediaType + "s can be created.", null); } if (accountNames != null) { if ((operation == null) || (!operation.equalsIgnoreCase("add") && !operation.equalsIgnoreCase("remove") && !operation.equalsIgnoreCase("reset"))) { throw new InvalidParameterValueException("Invalid operation on accounts, the operation must be either 'add' or 'remove' in order to modify launch permissions." - + " Given operation is: '" + operation + "'"); + + " Given operation is: '" + operation + "'", null); } } @@ -1384,7 +1392,7 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe if (accountId == null) { // if there is no owner of the template then it's probably already a public template (or domain private template) so // publishing to individual users is irrelevant - throw new InvalidParameterValueException("Update template permissions is an invalid operation on template " + template.getName()); + throw new InvalidParameterValueException("Update template permissions is an invalid operation on template " + template.getName(), null); } VMTemplateVO updatedTemplate = _tmpltDao.createForUpdate(); @@ -1396,14 +1404,14 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe if (isFeatured != null) { updatedTemplate.setFeatured(isFeatured.booleanValue()); } - - if (isExtractable != null && caller.getType() == Account.ACCOUNT_TYPE_ADMIN) {//Only ROOT admins allowed to change this powerful attribute - updatedTemplate.setExtractable(isExtractable.booleanValue()); - }else if (isExtractable != null && caller.getType() != Account.ACCOUNT_TYPE_ADMIN) { - throw new InvalidParameterValueException("Only ROOT admins are allowed to modify this attribute."); - } - _tmpltDao.update(template.getId(), updatedTemplate); + if (isExtractable != null && caller.getType() == Account.ACCOUNT_TYPE_ADMIN) {//Only ROOT admins allowed to change this powerful attribute + updatedTemplate.setExtractable(isExtractable.booleanValue()); + }else if (isExtractable != null && caller.getType() != Account.ACCOUNT_TYPE_ADMIN) { + throw new InvalidParameterValueException("Only ROOT admins are allowed to modify this attribute.", null); + } + + _tmpltDao.update(template.getId(), updatedTemplate); Long domainId = caller.getDomainId(); if ("add".equalsIgnoreCase(operation)) { @@ -1422,7 +1430,7 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe } else { txn.rollback(); throw new InvalidParameterValueException("Unable to grant a launch permission to account " + accountName + ", account not found. " - + "No permissions updated, please verify the account names and retry."); + + "No permissions updated, please verify the account names and retry.", null); } } txn.commit(); @@ -1446,5 +1454,5 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe } return true; } - + } diff --git a/server/src/com/cloud/user/DomainManagerImpl.java b/server/src/com/cloud/user/DomainManagerImpl.java index b0398bd8bc7..9c94a901361 100644 --- a/server/src/com/cloud/user/DomainManagerImpl.java +++ b/server/src/com/cloud/user/DomainManagerImpl.java @@ -133,7 +133,7 @@ public class DomainManagerImpl implements DomainManager, DomainService, Manager DomainVO parentDomain = _domainDao.findById(parentId); if (parentDomain == null) { - throw new InvalidParameterValueException("Unable to create domain " + name + ", parent domain " + parentId + " not found."); + throw new InvalidParameterValueException("Unable to create domain " + name + ", parent domain not found.", null); } if (parentDomain.getState().equals(Domain.State.Inactive)) { @@ -154,7 +154,7 @@ public class DomainManagerImpl implements DomainManager, DomainService, Manager if (!NetUtils.verifyDomainName(networkDomain)) { throw new InvalidParameterValueException( "Invalid network domain. Total length shouldn't exceed 190 chars. Each domain label must be between 1 and 63 characters long, can contain ASCII letters 'a' through 'z', the digits '0' through '9', " - + "and the hyphen ('-'); can't start or end with \"-\""); + + "and the hyphen ('-'); can't start or end with \"-\"", null); } } @@ -164,7 +164,7 @@ public class DomainManagerImpl implements DomainManager, DomainService, Manager List domains = _domainDao.search(sc, null); if (!domains.isEmpty()) { - throw new InvalidParameterValueException("Domain with name " + name + " already exists for the parent id=" + parentId); + throw new InvalidParameterValueException("Domain with name " + name + " already exists for the parent id=" + parentId, null); } Transaction txn = Transaction.currentTxn(); @@ -206,7 +206,7 @@ public class DomainManagerImpl implements DomainManager, DomainService, Manager DomainVO domain = _domainDao.findById(domainId); if (domain == null) { - throw new InvalidParameterValueException("Failed to delete domain " + domainId + ", domain not found"); + throw new InvalidParameterValueException("Failed to delete domain: domain not found", null); } else if (domainId == DomainVO.ROOT_DOMAIN) { throw new PermissionDeniedException("Can't delete ROOT domain"); } @@ -354,7 +354,7 @@ public class DomainManagerImpl implements DomainManager, DomainService, Manager if (domainId != null) { Domain domain = getDomain(domainId); if (domain == null) { - throw new InvalidParameterValueException("Domain id=" + domainId + " doesn't exist"); + throw new InvalidParameterValueException("Domain doesn't exist", null); } _accountMgr.checkAccess(caller, domain); } else { diff --git a/server/src/com/cloud/uuididentity/dao/IdentityDaoImpl.java b/server/src/com/cloud/uuididentity/dao/IdentityDaoImpl.java index fefe70b25bd..5341a166139 100644 --- a/server/src/com/cloud/uuididentity/dao/IdentityDaoImpl.java +++ b/server/src/com/cloud/uuididentity/dao/IdentityDaoImpl.java @@ -34,73 +34,75 @@ import com.cloud.utils.db.Transaction; @Local(value={IdentityDao.class}) public class IdentityDaoImpl extends GenericDaoBase implements IdentityDao { private static final Logger s_logger = Logger.getLogger(IdentityDaoImpl.class); - + public IdentityDaoImpl() { } - + + @Override @DB - public Long getIdentityId(IdentityMapper mapper, String identityString) { - assert(mapper.entityTableName() != null); - return getIdentityId(mapper.entityTableName(), identityString); - } - + public Long getIdentityId(IdentityMapper mapper, String identityString) { + assert(mapper.entityTableName() != null); + return getIdentityId(mapper.entityTableName(), identityString); + } + + @Override @DB public Long getIdentityId(String tableName, String identityString) { - assert(tableName != null); - assert(identityString != null); + assert(tableName != null); + assert(identityString != null); PreparedStatement pstmt = null; - Transaction txn = Transaction.open(Transaction.CLOUD_DB); - try { - try { - try { - pstmt = txn.prepareAutoCloseStatement(String.format("SELECT uuid FROM `%s`", tableName)); - pstmt.executeQuery(); - } catch (SQLException e) { - throw new InvalidParameterValueException("uuid field doesn't exist in table " + tableName); - } - - pstmt = txn.prepareAutoCloseStatement( - String.format("SELECT id FROM `%s` WHERE id=? OR uuid=?", tableName) - - // TODO : after graceful period, use following line turn on more secure check - // String.format("SELECT id FROM %s WHERE (id=? AND uuid IS NULL) OR uuid=?", mapper.entityTableName()) - ); - - long id = 0; - try { - // TODO : use regular expression to determine - id = Long.parseLong(identityString); - } catch(NumberFormatException e) { - // this could happen when it is a uuid string, so catch and ignore it - } - - pstmt.setLong(1, id); - pstmt.setString(2, identityString); - - ResultSet rs = pstmt.executeQuery(); - if(rs.next()) { - return rs.getLong(1); - } else { - if(id == -1L) - return id; - - throw new InvalidParameterValueException("Object " + tableName + "(uuid: " + identityString + ") does not exist."); - } - } catch (SQLException e) { - s_logger.error("Unexpected exception ", e); - } - } finally { - txn.close(); - } - return null; + Transaction txn = Transaction.open(Transaction.CLOUD_DB); + try { + try { + try { + pstmt = txn.prepareAutoCloseStatement(String.format("SELECT uuid FROM `%s`", tableName)); + pstmt.executeQuery(); + } catch (SQLException e) { + throw new InvalidParameterValueException("uuid field doesn't exist in table " + tableName, null); + } + + pstmt = txn.prepareAutoCloseStatement( + String.format("SELECT id FROM `%s` WHERE id=? OR uuid=?", tableName) + + // TODO : after graceful period, use following line turn on more secure check + // String.format("SELECT id FROM %s WHERE (id=? AND uuid IS NULL) OR uuid=?", mapper.entityTableName()) + ); + + long id = 0; + try { + // TODO : use regular expression to determine + id = Long.parseLong(identityString); + } catch(NumberFormatException e) { + // this could happen when it is a uuid string, so catch and ignore it + } + + pstmt.setLong(1, id); + pstmt.setString(2, identityString); + + ResultSet rs = pstmt.executeQuery(); + if(rs.next()) { + return rs.getLong(1); + } else { + if(id == -1L) + return id; + + throw new InvalidParameterValueException("Object " + tableName + "(uuid: " + identityString + ") does not exist.", null); + } + } catch (SQLException e) { + s_logger.error("Unexpected exception ", e); + } + } finally { + txn.close(); + } + return null; } - + @DB @Override public Pair getAccountDomainInfo(String tableName, Long identityId, TaggedResourceType resourceType) { assert(tableName != null); - + PreparedStatement pstmt = null; Transaction txn = Transaction.open(Transaction.CLOUD_DB); try { @@ -116,7 +118,7 @@ public class IdentityDaoImpl extends GenericDaoBase implements } } catch (SQLException e) { } - + //get accountId try { String account = "account_id"; @@ -136,55 +138,56 @@ public class IdentityDaoImpl extends GenericDaoBase implements txn.close(); } } - + @DB @Override - public String getIdentityUuid(String tableName, String identityString) { - assert(tableName != null); - assert(identityString != null); - + public String getIdentityUuid(String tableName, String identityString) { + assert(tableName != null); + assert(identityString != null); + PreparedStatement pstmt = null; - Transaction txn = Transaction.open(Transaction.CLOUD_DB); - try { - try { - pstmt = txn.prepareAutoCloseStatement( - String.format("SELECT uuid FROM `%s` WHERE id=? OR uuid=?", tableName) - // String.format("SELECT uuid FROM %s WHERE (id=? AND uuid IS NULL) OR uuid=?", tableName) - ); - - long id = 0; - try { - // TODO : use regular expression to determine - id = Long.parseLong(identityString); - } catch(NumberFormatException e) { - // this could happen when it is a uuid string, so catch and ignore it - } - - pstmt.setLong(1, id); - pstmt.setString(2, identityString); - - ResultSet rs = pstmt.executeQuery(); - if(rs.next()) { - String uuid = rs.getString(1); - if(uuid != null && !uuid.isEmpty()) - return uuid; - return identityString; - } - } catch (SQLException e) { - s_logger.error("Unexpected exception ", e); - } - } finally { - txn.close(); - } - - return identityString; - } - + Transaction txn = Transaction.open(Transaction.CLOUD_DB); + try { + try { + pstmt = txn.prepareAutoCloseStatement( + String.format("SELECT uuid FROM `%s` WHERE id=? OR uuid=?", tableName) + // String.format("SELECT uuid FROM %s WHERE (id=? AND uuid IS NULL) OR uuid=?", tableName) + ); + + long id = 0; + try { + // TODO : use regular expression to determine + id = Long.parseLong(identityString); + } catch(NumberFormatException e) { + // this could happen when it is a uuid string, so catch and ignore it + } + + pstmt.setLong(1, id); + pstmt.setString(2, identityString); + + ResultSet rs = pstmt.executeQuery(); + if(rs.next()) { + String uuid = rs.getString(1); + if(uuid != null && !uuid.isEmpty()) + return uuid; + return identityString; + } + } catch (SQLException e) { + s_logger.error("Unexpected exception ", e); + } + } finally { + txn.close(); + } + + return identityString; + } + + @Override @DB public void initializeDefaultUuid(String tableName) { assert(tableName != null); List l = getNullUuidRecords(tableName); - + Transaction txn = Transaction.open(Transaction.CLOUD_DB); try { try { @@ -201,19 +204,19 @@ public class IdentityDaoImpl extends GenericDaoBase implements txn.close(); } } - + @DB List getNullUuidRecords(String tableName) { List l = new ArrayList(); - + PreparedStatement pstmt = null; Transaction txn = Transaction.open(Transaction.CLOUD_DB); try { try { pstmt = txn.prepareAutoCloseStatement( - String.format("SELECT id FROM `%s` WHERE uuid IS NULL", tableName) - ); - + String.format("SELECT id FROM `%s` WHERE uuid IS NULL", tableName) + ); + ResultSet rs = pstmt.executeQuery(); while(rs.next()) { l.add(rs.getLong(1)); @@ -226,16 +229,16 @@ public class IdentityDaoImpl extends GenericDaoBase implements } return l; } - + @DB void setInitialUuid(String tableName, long id) throws SQLException { Transaction txn = Transaction.currentTxn(); - + PreparedStatement pstmtUpdate = null; pstmtUpdate = txn.prepareAutoCloseStatement( - String.format("UPDATE `%s` SET uuid=? WHERE id=?", tableName) - ); - + String.format("UPDATE `%s` SET uuid=? WHERE id=?", tableName) + ); + pstmtUpdate.setString(1, UUID.randomUUID().toString()); pstmtUpdate.setLong(2, id); pstmtUpdate.executeUpdate(); diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java index 606f69bf9b8..573c06ceae0 100755 --- a/server/src/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/com/cloud/vm/UserVmManagerImpl.java @@ -3590,7 +3590,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager // Validate physical network PhysicalNetwork physicalNetwork = _physicalNetworkDao.findById(physicalNetworkId); if (physicalNetwork == null) { - throw new InvalidParameterValueException("Unable to find physical network with id: "+physicalNetworkId + " and tag: " +requiredOfferings.get(0).getTags()); + throw new InvalidParameterValueException("Unable to find physical network by id and tag: " +requiredOfferings.get(0).getTags(), null); } s_logger.debug("Creating network for account " + newAccount + " from the network offering id=" + diff --git a/server/src/com/cloud/vm/VirtualMachineManagerImpl.java b/server/src/com/cloud/vm/VirtualMachineManagerImpl.java index f0570a98932..ae0b4937aa5 100755 --- a/server/src/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/server/src/com/cloud/vm/VirtualMachineManagerImpl.java @@ -281,7 +281,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene if (s_logger.isDebugEnabled()) { s_logger.debug("Allocating nics for " + vm); } - + try { _networkMgr.allocate(vmProfile, networks); } catch (ConcurrentOperationException e) { @@ -669,7 +669,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } continue; } - + StoragePoolVO pool = _storagePoolDao.findById(vol.getPoolId()); if (!pool.isInMaintenance()) { if (s_logger.isDebugEnabled()) { @@ -703,7 +703,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } } } - + VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vm, template, offering, account, params); DeployDestination dest = null; for (DeploymentPlanner planner : _planners) { @@ -754,7 +754,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene if(!reuseVolume){ reuseVolume = true; } - + Commands cmds = null; vmGuru.finalizeVirtualMachineProfile(vmProfile, dest, ctx); @@ -773,10 +773,10 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene _workDao.updateStep(work, Step.Starting); _agentMgr.send(destHostId, cmds); - + _workDao.updateStep(work, Step.Started); - - + + StartAnswer startAnswer = cmds.getAnswer(StartAnswer.class); if (startAnswer != null && startAnswer.getResult()) { String host_guid = startAnswer.getHost_guid(); @@ -800,7 +800,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene if (s_logger.isDebugEnabled()) { s_logger.info("The guru did not like the answers so stopping " + vm); } - + StopCommand cmd = new StopCommand(vm.getInstanceName()); StopAnswer answer = (StopAnswer) _agentMgr.easySend(destHostId, cmd); if (answer == null || !answer.getResult()) { @@ -809,14 +809,14 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene throw new ExecutionException("Unable to stop " + vm + " so we are unable to retry the start operation"); } if (vmGuru.recreateNeeded(vmProfile, destHostId, cmds, ctx)) { - recreate = true; + recreate = true; } else { - throw new ExecutionException("Unable to start " + vm + " due to error in finalizeStart, not retrying"); + throw new ExecutionException("Unable to start " + vm + " due to error in finalizeStart, not retrying"); } } } s_logger.info("Unable to start VM on " + dest.getHost() + " due to " + (startAnswer == null ? " no start answer" : startAnswer.getDetails())); - + } catch (OperationTimedoutException e) { s_logger.debug("Unable to send the start command to host " + dest.getHost()); if (e.isActive()) { @@ -1073,7 +1073,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } vmGuru.prepareStop(profile); - + StopCommand stop = new StopCommand(vm, vm.getInstanceName(), null); boolean stopped = false; StopAnswer answer = null; @@ -1644,7 +1644,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene commands.addCommand(command); } } - + for (final AgentVmInfo left : infos.values()) { boolean found = false; for (VirtualMachineGuru vmGuru : _vmGurus.values()) { @@ -1740,7 +1740,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene public void fullSync(final long clusterId, Map> newStates) { - if (newStates==null)return; + if (newStates==null)return; Map infos = convertToInfos(newStates); Set set_vms = Collections.synchronizedSet(new HashSet()); set_vms.addAll(_vmDao.listByClusterId(clusterId)); @@ -1751,11 +1751,11 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene AgentVmInfo info = infos.remove(vm.getId()); VMInstanceVO castedVm = null; if ((info == null && (vm.getState() == State.Running || vm.getState() == State.Starting)) - || (info != null && (info.state == State.Running && vm.getState() == State.Starting))) + || (info != null && (info.state == State.Running && vm.getState() == State.Starting))) { - s_logger.info("Found vm " + vm.getInstanceName() + " in inconsistent state. " + vm.getState() + " on CS while " + (info == null ? "Stopped" : "Running") + " on agent"); + s_logger.info("Found vm " + vm.getInstanceName() + " in inconsistent state. " + vm.getState() + " on CS while " + (info == null ? "Stopped" : "Running") + " on agent"); info = new AgentVmInfo(vm.getInstanceName(), getVmGuru(vm), vm, State.Stopped); - + // Bug 13850- grab outstanding work item if any for this VM state so that we mark it as DONE after we change VM state, else it will remain pending ItWorkVO work = _workDao.findByOutstandingWork(vm.getId(), vm.getState()); if (work != null) { @@ -1764,8 +1764,8 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } } vm.setState(State.Running); // set it as running and let HA take care of it - _vmDao.persist(vm); - + _vmDao.persist(vm); + if (work != null) { if (s_logger.isDebugEnabled()) { s_logger.debug("Updating outstanding work item to Done, id:" + work.getId()); @@ -1773,7 +1773,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene work.setStep(Step.Done); _workDao.update(work.getId(), work); } - + castedVm = info.guru.findById(vm.getId()); try { Host host = _hostDao.findByGuid(info.getHostUuid()); @@ -1792,38 +1792,38 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } } else if (info != null && (vm.getState() == State.Stopped || vm.getState() == State.Stopping)) { - Host host = _hostDao.findByGuid(info.getHostUuid()); - if (host != null){ + Host host = _hostDao.findByGuid(info.getHostUuid()); + if (host != null){ s_logger.warn("Stopping a VM which is stopped/stopping " + info.name); vm.setState(State.Stopped); // set it as stop and clear it from host vm.setHostId(null); _vmDao.persist(vm); - try { - Answer answer = _agentMgr.send(host.getId(), cleanup(info.name)); - if (!answer.getResult()) { - s_logger.warn("Unable to stop a VM due to " + answer.getDetails()); - } - } - catch (Exception e) { - s_logger.warn("Unable to stop a VM due to " + e.getMessage()); - } - } + try { + Answer answer = _agentMgr.send(host.getId(), cleanup(info.name)); + if (!answer.getResult()) { + s_logger.warn("Unable to stop a VM due to " + answer.getDetails()); + } + } + catch (Exception e) { + s_logger.warn("Unable to stop a VM due to " + e.getMessage()); + } + } } else - // host id can change - if (info != null && vm.getState() == State.Running){ - // check for host id changes - Host host = _hostDao.findByGuid(info.getHostUuid()); - if (host != null && (vm.getHostId() == null || host.getId() != vm.getHostId())){ - s_logger.info("Found vm " + vm.getInstanceName() + " with inconsistent host in db, new host is " + host.getId()); - try { - stateTransitTo(vm, VirtualMachine.Event.AgentReportMigrated, host.getId()); - } catch (NoTransitionException e) { - s_logger.warn(e.getMessage()); - } - } - } - /* else if(info == null && vm.getState() == State.Stopping) { //Handling CS-13376 + // host id can change + if (info != null && vm.getState() == State.Running){ + // check for host id changes + Host host = _hostDao.findByGuid(info.getHostUuid()); + if (host != null && (vm.getHostId() == null || host.getId() != vm.getHostId())){ + s_logger.info("Found vm " + vm.getInstanceName() + " with inconsistent host in db, new host is " + host.getId()); + try { + stateTransitTo(vm, VirtualMachine.Event.AgentReportMigrated, host.getId()); + } catch (NoTransitionException e) { + s_logger.warn(e.getMessage()); + } + } + } + /* else if(info == null && vm.getState() == State.Stopping) { //Handling CS-13376 s_logger.warn("Marking the VM as Stopped as it was still stopping on the CS" +vm.getName()); vm.setState(State.Stopped); // Setting the VM as stopped on the DB and clearing it from the host vm.setLastHostId(vm.getHostId()); @@ -1833,7 +1833,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } for (final AgentVmInfo left : infos.values()) { - if (VirtualMachineName.isValidVmName(left.name)) continue; // if the vm follows cloudstack naming ignore it for stopping + if (VirtualMachineName.isValidVmName(left.name)) continue; // if the vm follows cloudstack naming ignore it for stopping try { Host host = _hostDao.findByGuid(left.getHostUuid()); if (host != null){ @@ -1861,7 +1861,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene boolean is_alien_vm = true; long alien_vm_count = -1; for (Map.Entry> entry : newStates.entrySet()) { - is_alien_vm = true; + is_alien_vm = true; for (VirtualMachineGuru vmGuru : vmGurus) { String name = entry.getKey(); VMInstanceVO vm = vmGuru.findByName(name); @@ -1879,8 +1879,8 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } // alien VMs if (is_alien_vm){ - map.put(alien_vm_count--, new AgentVmInfo(entry.getKey(), null, null, entry.getValue().second(), entry.getValue().first())); - s_logger.warn("Found an alien VM " + entry.getKey()); + map.put(alien_vm_count--, new AgentVmInfo(entry.getKey(), null, null, entry.getValue().second(), entry.getValue().first())); + s_logger.warn("Found an alien VM " + entry.getKey()); } } return map; @@ -2262,13 +2262,13 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene Long clusterId = agent.getClusterId(); long agentId = agent.getId(); if (agent.getHypervisorType() == HypervisorType.XenServer) { // only for Xen - StartupRoutingCommand startup = (StartupRoutingCommand) cmd; - HashMap> allStates = startup.getClusterVMStateChanges(); - if (allStates != null){ - this.fullSync(clusterId, allStates); - } - - // initiate the cron job + StartupRoutingCommand startup = (StartupRoutingCommand) cmd; + HashMap> allStates = startup.getClusterVMStateChanges(); + if (allStates != null){ + this.fullSync(clusterId, allStates); + } + + // initiate the cron job ClusterSyncCommand syncCmd = new ClusterSyncCommand(Integer.parseInt(Config.ClusterDeltaSyncInterval.getDefaultValue()), clusterId); try { long seq_no = _agentMgr.send(agentId, new Commands(syncCmd), this); @@ -2370,31 +2370,31 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene public VMInstanceVO findById(long vmId) { return _vmDao.findById(vmId); } - + @Override public void checkIfCanUpgrade(VirtualMachine vmInstance, long newServiceOfferingId) { ServiceOfferingVO newServiceOffering = _offeringDao.findById(newServiceOfferingId); if (newServiceOffering == null) { - throw new InvalidParameterValueException("Unable to find a service offering with id " + newServiceOfferingId); + throw new InvalidParameterValueException("Unable to find a service offering by id", null); } // Check that the VM is stopped if (!vmInstance.getState().equals(State.Stopped)) { s_logger.warn("Unable to upgrade virtual machine " + vmInstance.toString() + " in state " + vmInstance.getState()); throw new InvalidParameterValueException("Unable to upgrade virtual machine " + vmInstance.toString() + " " + - "in state " + vmInstance.getState() - + "; make sure the virtual machine is stopped and not in an error state before upgrading."); + "in state " + vmInstance.getState() + + "; make sure the virtual machine is stopped and not in an error state before upgrading.", null); } // Check if the service offering being upgraded to is what the VM is already running with if (vmInstance.getServiceOfferingId() == newServiceOffering.getId()) { if (s_logger.isInfoEnabled()) { s_logger.info("Not upgrading vm " + vmInstance.toString() + " since it already has the requested " + - "service offering (" + newServiceOffering.getName() + ")"); + "service offering (" + newServiceOffering.getName() + ")"); } throw new InvalidParameterValueException("Not upgrading vm " + vmInstance.toString() + " since it already " + - "has the requested service offering (" + newServiceOffering.getName() + ")"); + "has the requested service offering (" + newServiceOffering.getName() + ")", null); } ServiceOfferingVO currentServiceOffering = _offeringDao.findByIdIncludingRemoved(vmInstance.getServiceOfferingId()); @@ -2414,19 +2414,19 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene throw new InvalidParameterValueException("Unable to upgrade virtual machine " + vmInstance.toString() + ", cannot switch between local storage and shared storage service offerings. Current offering " + "useLocalStorage=" + currentServiceOffering.getUseLocalStorage() - + ", target offering useLocalStorage=" + newServiceOffering.getUseLocalStorage()); + + ", target offering useLocalStorage=" + newServiceOffering.getUseLocalStorage(), null); } - + // if vm is a system vm, check if it is a system service offering, if yes return with error as it cannot be used for user vms if (currentServiceOffering.getSystemUse() != newServiceOffering.getSystemUse()) { - throw new InvalidParameterValueException("isSystem property is different for current service offering and new service offering"); + throw new InvalidParameterValueException("isSystem property is different for current service offering and new service offering", null); } // Check that there are enough resources to upgrade the service offering if (!isVirtualMachineUpgradable(vmInstance, newServiceOffering)) { throw new InvalidParameterValueException("Unable to upgrade virtual machine, not enough resources available " + - "for an offering of " + newServiceOffering.getCpu() + " cpu(s) at " - + newServiceOffering.getSpeed() + " Mhz, and " + newServiceOffering.getRamSize() + " MB of memory"); + "for an offering of " + newServiceOffering.getCpu() + " cpu(s) at " + + newServiceOffering.getSpeed() + " Mhz, and " + newServiceOffering.getRamSize() + " MB of memory", null); } // Check that the service offering being upgraded to has all the tags of the current service offering @@ -2434,12 +2434,12 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene List newTags = _configMgr.csvTagsToList(newServiceOffering.getTags()); if (!newTags.containsAll(currentTags)) { throw new InvalidParameterValueException("Unable to upgrade virtual machine; the new service offering " + - "does not have all the tags of the " + "does not have all the tags of the " + "current service offering. Current service offering tags: " + currentTags + "; " + "new service " + - "offering tags: " + newTags); + "offering tags: " + newTags, null); } } - + @Override public boolean upgradeVmDb(long vmId, long serviceOfferingId) { VMInstanceVO vmForUpdate = _vmDao.createForUpdate(); @@ -2450,38 +2450,38 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene vmForUpdate.setServiceOfferingId(newSvcOff.getId()); return _vmDao.update(vmId, vmForUpdate); } - + @Override public NicProfile addVmToNetwork(VirtualMachine vm, Network network, NicProfile requested) throws ConcurrentOperationException, - ResourceUnavailableException, InsufficientCapacityException { - + ResourceUnavailableException, InsufficientCapacityException { + s_logger.debug("Adding vm " + vm + " to network " + network + "; requested nic profile " + requested); VMInstanceVO vmVO = _vmDao.findById(vm.getId()); ReservationContext context = new ReservationContextImpl(null, null, _accountMgr.getActiveUser(User.UID_SYSTEM), _accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM)); - + VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vmVO, null, null, null, null); - + DataCenter dc = _configMgr.getZone(network.getDataCenterId()); Host host = _hostDao.findById(vm.getHostId()); DeployDestination dest = new DeployDestination(dc, null, null, host); - + //check vm state if (vm.getState() == State.Running) { //1) allocate and prepare nic NicProfile nic = _networkMgr.createNicForVm(network, requested, context, vmProfile, true); - + //2) Convert vmProfile to vmTO HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vmProfile.getVirtualMachine().getHypervisorType()); VirtualMachineTO vmTO = hvGuru.implement(vmProfile); - + //3) Convert nicProfile to NicTO NicTO nicTO = toNicTO(nic, vmProfile.getVirtualMachine().getHypervisorType()); - + //4) plug the nic to the vm VirtualMachineGuru vmGuru = getVmGuru(vmVO); - + s_logger.debug("Plugging nic for vm " + vm + " in network " + network); if (vmGuru.plugNic(network, nicTO, vmTO, context, dest)) { s_logger.debug("Nic is plugged successfully for vm " + vm + " in network " + network + ". Vm is a part of network now"); @@ -2504,40 +2504,40 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene @Override public NicTO toNicTO(NicProfile nic, HypervisorType hypervisorType) { HypervisorGuru hvGuru = _hvGuruMgr.getGuru(hypervisorType); - + NicTO nicTO = hvGuru.toNicTO(nic); return nicTO; } - + @Override public boolean removeVmFromNetwork(VirtualMachine vm, Network network, URI broadcastUri) throws ConcurrentOperationException, ResourceUnavailableException { VMInstanceVO vmVO = _vmDao.findById(vm.getId()); ReservationContext context = new ReservationContextImpl(null, null, _accountMgr.getActiveUser(User.UID_SYSTEM), _accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM)); - + VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vmVO, null, null, null, null); - + DataCenter dc = _configMgr.getZone(network.getDataCenterId()); Host host = _hostDao.findById(vm.getHostId()); DeployDestination dest = new DeployDestination(dc, null, null, host); VirtualMachineGuru vmGuru = getVmGuru(vmVO); HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vmProfile.getVirtualMachine().getHypervisorType()); VirtualMachineTO vmTO = hvGuru.implement(vmProfile); - + Nic nic = null; - + if (broadcastUri != null) { nic = _nicsDao.findByNetworkIdInstanceIdAndBroadcastUri(network.getId(), vm.getId(), broadcastUri.toString()); } else { nic = _networkMgr.getNicInNetwork(vm.getId(), network.getId()); } - + NicProfile nicProfile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), _networkMgr.getNetworkRate(network.getId(), vm.getId()), _networkMgr.isSecurityGroupSupportedInNetwork(network), _networkMgr.getNetworkTag(vmProfile.getVirtualMachine().getHypervisorType(), network)); - + //1) Unplug the nic NicTO nicTO = toNicTO(nicProfile, vmProfile.getVirtualMachine().getHypervisorType()); s_logger.debug("Un-plugging nic for vm " + vm + " from network " + network); @@ -2548,14 +2548,14 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene s_logger.warn("Failed to unplug nic for the vm " + vm + " from network " + network); return false; } - + //2) Release the nic _networkMgr.releaseNic(vmProfile, nic); s_logger.debug("Successfully released nic " + nic + "for vm " + vm); - + //3) Remove the nic _networkMgr.removeNic(vmProfile, nic); return result; } - + }