diff --git a/agent-simulator/src/com/cloud/agent/manager/MockAgentManager.java b/agent-simulator/src/com/cloud/agent/manager/MockAgentManager.java index a97b68017ee..8900bb2d33f 100644 --- a/agent-simulator/src/com/cloud/agent/manager/MockAgentManager.java +++ b/agent-simulator/src/com/cloud/agent/manager/MockAgentManager.java @@ -46,11 +46,11 @@ public interface MockAgentManager extends Manager { Answer checkHealth(CheckHealthCommand cmd); Answer pingTest(PingTestCommand cmd); - Answer PrepareForMigration(PrepareForMigrationCommand cmd); + Answer prepareForMigrate(PrepareForMigrationCommand cmd); MockHost getHost(String guid); - Answer MaintainCommand(MaintainCommand cmd); + Answer maintain(MaintainCommand cmd); Answer checkNetworkCommand(CheckNetworkCommand cmd); } diff --git a/agent-simulator/src/com/cloud/agent/manager/MockAgentManagerImpl.java b/agent-simulator/src/com/cloud/agent/manager/MockAgentManagerImpl.java index 7fb96dd7310..e83e2aa598e 100755 --- a/agent-simulator/src/com/cloud/agent/manager/MockAgentManagerImpl.java +++ b/agent-simulator/src/com/cloud/agent/manager/MockAgentManagerImpl.java @@ -41,10 +41,9 @@ import com.cloud.agent.api.MaintainAnswer; import com.cloud.agent.api.PingTestCommand; import com.cloud.agent.api.PrepareForMigrationAnswer; import com.cloud.agent.api.PrepareForMigrationCommand; -import com.cloud.agent.api.StartupCommand; +import com.cloud.agent.api.to.VirtualMachineTO; import com.cloud.dc.dao.HostPodDao; import com.cloud.host.Host; -import com.cloud.host.Host.Type; import com.cloud.resource.AgentResourceBase; import com.cloud.resource.AgentRoutingResource; import com.cloud.resource.AgentStorageResource; @@ -336,7 +335,11 @@ public class MockAgentManagerImpl implements MockAgentManager { @Override - public PrepareForMigrationAnswer PrepareForMigration(PrepareForMigrationCommand cmd) { + public PrepareForMigrationAnswer prepareForMigrate(PrepareForMigrationCommand cmd) { + VirtualMachineTO vm = cmd.getVirtualMachine(); + if (s_logger.isDebugEnabled()) { + s_logger.debug("Preparing host for migrating " + vm); + } return new PrepareForMigrationAnswer(cmd); } @@ -360,7 +363,7 @@ public class MockAgentManagerImpl implements MockAgentManager { @Override - public MaintainAnswer MaintainCommand(com.cloud.agent.api.MaintainCommand cmd) { + public MaintainAnswer maintain(com.cloud.agent.api.MaintainCommand cmd) { return new MaintainAnswer(cmd); } diff --git a/agent-simulator/src/com/cloud/agent/manager/MockVmManager.java b/agent-simulator/src/com/cloud/agent/manager/MockVmManager.java index 10ae30096e4..d7dd76569f6 100644 --- a/agent-simulator/src/com/cloud/agent/manager/MockVmManager.java +++ b/agent-simulator/src/com/cloud/agent/manager/MockVmManager.java @@ -51,7 +51,7 @@ public interface MockVmManager extends Manager { public Answer stopVM(StopCommand cmd); public Answer rebootVM(RebootCommand cmd); - public Answer checkVmState(CheckVirtualMachineCommand cmd, String hostGuid); + public Answer checkVmState(CheckVirtualMachineCommand cmd); public Map getVmStates(String hostGuid); public Answer getVncPort(GetVncPortCommand cmd); diff --git a/agent-simulator/src/com/cloud/agent/manager/MockVmManagerImpl.java b/agent-simulator/src/com/cloud/agent/manager/MockVmManagerImpl.java index cc98cf9371e..fff9381bd66 100644 --- a/agent-simulator/src/com/cloud/agent/manager/MockVmManagerImpl.java +++ b/agent-simulator/src/com/cloud/agent/manager/MockVmManagerImpl.java @@ -239,8 +239,8 @@ public class MockVmManagerImpl implements MockVmManager { } @Override - public CheckVirtualMachineAnswer checkVmState(CheckVirtualMachineCommand cmd, String hostGuid) { - MockVMVO vm = _mockVmDao.findByVmNameAndHost(cmd.getVmName(), hostGuid); + public CheckVirtualMachineAnswer checkVmState(CheckVirtualMachineCommand cmd) { + MockVMVO vm = _mockVmDao.findByVmName(cmd.getVmName()); if (vm == null) { return new CheckVirtualMachineAnswer(cmd, "can't find vm:" + cmd.getVmName()); } diff --git a/agent-simulator/src/com/cloud/agent/manager/SimulatorManagerImpl.java b/agent-simulator/src/com/cloud/agent/manager/SimulatorManagerImpl.java index 1517c94d91a..a1233dfeb28 100644 --- a/agent-simulator/src/com/cloud/agent/manager/SimulatorManagerImpl.java +++ b/agent-simulator/src/com/cloud/agent/manager/SimulatorManagerImpl.java @@ -15,7 +15,6 @@ package com.cloud.agent.manager; import java.sql.Connection; import java.sql.SQLException; import java.util.HashMap; -import java.util.List; import java.util.Map; import javax.ejb.Local; @@ -29,6 +28,7 @@ import com.cloud.agent.api.AttachVolumeCommand; import com.cloud.agent.api.BackupSnapshotCommand; import com.cloud.agent.api.CheckHealthCommand; import com.cloud.agent.api.CheckNetworkCommand; +import com.cloud.agent.api.CheckVirtualMachineCommand; import com.cloud.agent.api.CleanupNetworkRulesCmd; import com.cloud.agent.api.ClusterSyncCommand; import com.cloud.agent.api.Command; @@ -50,6 +50,7 @@ import com.cloud.agent.api.MigrateCommand; import com.cloud.agent.api.ModifyStoragePoolCommand; import com.cloud.agent.api.NetworkUsageCommand; import com.cloud.agent.api.PingTestCommand; +import com.cloud.agent.api.PrepareForMigrationCommand; import com.cloud.agent.api.RebootCommand; import com.cloud.agent.api.SecStorageSetupCommand; import com.cloud.agent.api.SecStorageVMSetupCommand; @@ -76,7 +77,6 @@ import com.cloud.agent.api.storage.DownloadCommand; import com.cloud.agent.api.storage.DownloadProgressCommand; import com.cloud.agent.api.storage.ListTemplateCommand; import com.cloud.agent.api.storage.PrimaryStorageDownloadCommand; -import com.cloud.agent.mockvm.MockVm; import com.cloud.simulator.MockConfigurationVO; import com.cloud.simulator.MockHost; import com.cloud.simulator.MockVMVO; @@ -193,12 +193,16 @@ public class SimulatorManagerImpl implements SimulatorManager { return _mockAgentMgr.checkHealth((CheckHealthCommand)cmd); } else if (cmd instanceof PingTestCommand) { return _mockAgentMgr.pingTest((PingTestCommand)cmd); + } else if (cmd instanceof PrepareForMigrationCommand) { + return _mockAgentMgr.prepareForMigrate((PrepareForMigrationCommand)cmd); } else if (cmd instanceof MigrateCommand) { return _mockVmMgr.Migrate((MigrateCommand)cmd, info); } else if (cmd instanceof StartCommand) { return _mockVmMgr.startVM((StartCommand)cmd, info); } else if (cmd instanceof CheckSshCommand) { return _mockVmMgr.checkSshCommand((CheckSshCommand)cmd); + } else if (cmd instanceof CheckVirtualMachineCommand) { + return _mockVmMgr.checkVmState((CheckVirtualMachineCommand)cmd); } else if (cmd instanceof SetStaticNatRulesCommand) { return _mockVmMgr.SetStaticNatRules((SetStaticNatRulesCommand)cmd); } else if (cmd instanceof SetFirewallRulesCommand) { @@ -278,7 +282,7 @@ public class SimulatorManagerImpl implements SimulatorManager { } else if (cmd instanceof CreatePrivateTemplateFromVolumeCommand) { return _mockStorageMgr.CreatePrivateTemplateFromVolume((CreatePrivateTemplateFromVolumeCommand)cmd); } else if (cmd instanceof MaintainCommand) { - return _mockAgentMgr.MaintainCommand((MaintainCommand)cmd); + return _mockAgentMgr.maintain((MaintainCommand)cmd); } else if (cmd instanceof GetVmStatsCommand) { return _mockVmMgr.getVmStats((GetVmStatsCommand)cmd); } else if (cmd instanceof GetDomRVersionCmd) { diff --git a/agent-simulator/src/com/cloud/resource/AgentRoutingResource.java b/agent-simulator/src/com/cloud/resource/AgentRoutingResource.java index fe4534f681a..24b6cbd174c 100644 --- a/agent-simulator/src/com/cloud/resource/AgentRoutingResource.java +++ b/agent-simulator/src/com/cloud/resource/AgentRoutingResource.java @@ -29,6 +29,7 @@ import com.cloud.agent.api.CheckVirtualMachineCommand; import com.cloud.agent.api.Command; import com.cloud.agent.api.PingCommand; import com.cloud.agent.api.PingRoutingWithNwGroupsCommand; +import com.cloud.agent.api.PrepareForMigrationAnswer; import com.cloud.agent.api.PrepareForMigrationCommand; import com.cloud.agent.api.ReadyAnswer; import com.cloud.agent.api.ReadyCommand; @@ -81,8 +82,6 @@ public class AgentRoutingResource extends AgentStorageResource { return execute((StartCommand) cmd); } else if (cmd instanceof StopCommand) { return execute((StopCommand) cmd); - } else if (cmd instanceof PrepareForMigrationCommand) { - return execute((PrepareForMigrationCommand) cmd); } else if (cmd instanceof CheckVirtualMachineCommand) { return execute((CheckVirtualMachineCommand) cmd); } else if (cmd instanceof ReadyCommand) { @@ -187,7 +186,7 @@ public class AgentRoutingResource extends AgentStorageResource { String vmName = vmSpec.getName(); if (this.totalCpu < (vmSpec.getCpus() * vmSpec.getSpeed() + this.usedCpu) || this.totalMem < (vmSpec.getMaxRam() + this.usedMem)) { - return new StartAnswer(cmd, "No enough resource to start the vm"); + return new StartAnswer(cmd, "Not enough resource to start the vm"); } State state = State.Stopped; synchronized (_vms) { diff --git a/api/src/com/cloud/agent/api/NetworkUsageCommand.java b/api/src/com/cloud/agent/api/NetworkUsageCommand.java index 14621ffd1dc..324e1a590eb 100644 --- a/api/src/com/cloud/agent/api/NetworkUsageCommand.java +++ b/api/src/com/cloud/agent/api/NetworkUsageCommand.java @@ -54,6 +54,15 @@ public class NetworkUsageCommand extends Command { this.vpcCIDR = vpcCIDR; } + public NetworkUsageCommand(String privateIP, String domRName, String option, boolean forVpc, String gatewayIP) + { + this.privateIP = privateIP; + this.domRName = domRName; + this.forVpc = forVpc; + this.gatewayIP = gatewayIP; + this.option = option; + } + public String getPrivateIP() { return privateIP; } diff --git a/api/src/com/cloud/api/BaseCmd.java b/api/src/com/cloud/api/BaseCmd.java index 2ca70577c14..faa4d5b71eb 100755 --- a/api/src/com/cloud/api/BaseCmd.java +++ b/api/src/com/cloud/api/BaseCmd.java @@ -36,7 +36,7 @@ import com.cloud.exception.ResourceAllocationException; import com.cloud.exception.ResourceUnavailableException; import com.cloud.network.NetworkService; import com.cloud.network.StorageNetworkService; -import com.cloud.network.VirtualNetworkApplianceService; +import com.cloud.network.VpcVirtualNetworkApplianceService; import com.cloud.network.as.AutoScaleService; import com.cloud.network.firewall.FirewallService; import com.cloud.network.firewall.NetworkACLService; @@ -117,7 +117,7 @@ public abstract class BaseCmd { public static SecurityGroupService _securityGroupService; public static SnapshotService _snapshotService; public static ConsoleProxyService _consoleProxyService; - public static VirtualNetworkApplianceService _routerService; + public static VpcVirtualNetworkApplianceService _routerService; public static ResponseGenerator _responseGenerator; public static EntityManager _entityMgr; public static RulesService _rulesService; @@ -149,7 +149,7 @@ public abstract class BaseCmd { _securityGroupService = locator.getManager(SecurityGroupService.class); _snapshotService = locator.getManager(SnapshotService.class); _consoleProxyService = locator.getManager(ConsoleProxyService.class); - _routerService = locator.getManager(VirtualNetworkApplianceService.class); + _routerService = locator.getManager(VpcVirtualNetworkApplianceService.class); _entityMgr = locator.getManager(EntityManager.class); _rulesService = locator.getManager(RulesService.class); _lbService = locator.getManager(LoadBalancingRulesService.class); diff --git a/api/src/com/cloud/api/commands/CreateVPCCmd.java b/api/src/com/cloud/api/commands/CreateVPCCmd.java index 9b5fd4677b3..53e9947287b 100644 --- a/api/src/com/cloud/api/commands/CreateVPCCmd.java +++ b/api/src/com/cloud/api/commands/CreateVPCCmd.java @@ -52,6 +52,10 @@ public class CreateVPCCmd extends BaseAsyncCreateCmd{ "If used with the account parameter returns the VPC associated with the account for the specified domain.") private Long domainId; + @IdentityMapper(entityTableName="projects") + @Parameter(name=ApiConstants.PROJECT_ID, type=CommandType.LONG, description="create VPC for the project") + private Long projectId; + @IdentityMapper(entityTableName="data_center") @Parameter(name=ApiConstants.ZONE_ID, type=CommandType.LONG, required=true, description="the ID of the availability zone") private Long zoneId; @@ -72,7 +76,8 @@ public class CreateVPCCmd extends BaseAsyncCreateCmd{ @Parameter(name=ApiConstants.VPC_OFF_ID, type=CommandType.LONG, required=true, description="the ID of the VPC offering") private Long vpcOffering; - @Parameter(name=ApiConstants.NETWORK_DOMAIN, type=CommandType.STRING, description="network domain") + @Parameter(name=ApiConstants.NETWORK_DOMAIN, type=CommandType.STRING, + description="VPC network domain. All networks inside the VPC will belong to this domain") private String networkDomain; ///////////////////////////////////////////////////// @@ -174,7 +179,7 @@ public class CreateVPCCmd extends BaseAsyncCreateCmd{ @Override public long getEntityOwnerId() { - Long accountId = finalyzeAccountId(accountName, domainId, null, true); + Long accountId = finalyzeAccountId(accountName, domainId, projectId, true); if (accountId == null) { return UserContext.current().getCaller().getId(); } diff --git a/api/src/com/cloud/api/commands/ListVPCsCmd.java b/api/src/com/cloud/api/commands/ListVPCsCmd.java index e60c6e290f6..803aee689da 100644 --- a/api/src/com/cloud/api/commands/ListVPCsCmd.java +++ b/api/src/com/cloud/api/commands/ListVPCsCmd.java @@ -137,7 +137,7 @@ public class ListVPCsCmd extends BaseListTaggedResourcesCmd{ List vpcs = _vpcService.listVpcs(getId(), getVpcName(), getDisplayText(), getSupportedServices(), getCidr(), getVpcOffId(), getState(), getAccountName(), getDomainId(), this.getKeyword(), this.getStartIndex(), this.getPageSizeVal(), getZoneId(), this.isRecursive(), - this.listAll(), getRestartRequired(), getTags()); + this.listAll(), getRestartRequired(), getTags(), getProjectId()); ListResponse response = new ListResponse(); List offeringResponses = new ArrayList(); for (Vpc vpc : vpcs) { diff --git a/api/src/com/cloud/api/response/AsyncJobResponse.java b/api/src/com/cloud/api/response/AsyncJobResponse.java index b2176793cb6..bcdce390a2a 100644 --- a/api/src/com/cloud/api/response/AsyncJobResponse.java +++ b/api/src/com/cloud/api/response/AsyncJobResponse.java @@ -126,7 +126,7 @@ public class AsyncJobResponse extends BaseResponse { this.jobInstanceId.setTableName("account"); } else if (jobInstanceType.equalsIgnoreCase(AsyncJob.Type.User.toString())) { this.jobInstanceId.setTableName("user"); - }else if (jobInstanceType.equalsIgnoreCase(AsyncJob.Type.Counter.toString())) { + } else if (jobInstanceType.equalsIgnoreCase(AsyncJob.Type.Counter.toString())) { this.jobInstanceId.setTableName("counter"); } else if (jobInstanceType.equalsIgnoreCase(AsyncJob.Type.Condition.toString())) { this.jobInstanceId.setTableName("conditions"); diff --git a/api/src/com/cloud/api/response/DiskOfferingResponse.java b/api/src/com/cloud/api/response/DiskOfferingResponse.java index b2a34a1d862..6847646a4f1 100644 --- a/api/src/com/cloud/api/response/DiskOfferingResponse.java +++ b/api/src/com/cloud/api/response/DiskOfferingResponse.java @@ -47,6 +47,9 @@ public class DiskOfferingResponse extends BaseResponse { @SerializedName(ApiConstants.TAGS) @Param(description="the tags for the disk offering") private String tags; + @SerializedName("storagetype") @Param(description="the storage type for this disk offering") + private String storageType; + public Long getId() { return id.getValue(); } @@ -119,4 +122,11 @@ public class DiskOfferingResponse extends BaseResponse { this.customized = customized; } + public String getStorageType() { + return storageType; + } + + public void setStorageType(String storageType) { + this.storageType = storageType; + } } diff --git a/api/src/com/cloud/api/response/VpcResponse.java b/api/src/com/cloud/api/response/VpcResponse.java index 8d27ca4a7c9..149cbc30359 100644 --- a/api/src/com/cloud/api/response/VpcResponse.java +++ b/api/src/com/cloud/api/response/VpcResponse.java @@ -74,10 +74,10 @@ public class VpcResponse extends BaseResponse implements ControlledEntityRespons @SerializedName(ApiConstants.NETWORK) @Param(description="the list of networks belongign to the VPC", responseObject = NetworkResponse.class) private List networks; - @SerializedName(ApiConstants.RESTART_REQUIRED) @Param(description="true network requires restart") + @SerializedName(ApiConstants.RESTART_REQUIRED) @Param(description="true VPC requires restart") private Boolean restartRequired; - @SerializedName(ApiConstants.NETWORK_DOMAIN) @Param(description="the network domain") + @SerializedName(ApiConstants.NETWORK_DOMAIN) @Param(description="the network domain of the VPC") private String networkDomain; @SerializedName(ApiConstants.TAGS) @Param(description="the list of resource tags associated with the project", responseObject = ResourceTagResponse.class) diff --git a/api/src/com/cloud/event/EventTypes.java b/api/src/com/cloud/event/EventTypes.java index 611de42ccf5..250a5e1c9ea 100755 --- a/api/src/com/cloud/event/EventTypes.java +++ b/api/src/com/cloud/event/EventTypes.java @@ -301,5 +301,5 @@ public class EventTypes { public static final String EVENT_AUTOSCALEVMGROUP_DELETE = "AUTOSCALEVMGROUP.DELETE"; public static final String EVENT_AUTOSCALEVMGROUP_UPDATE = "AUTOSCALEVMGROUP.UPDATE"; public static final String EVENT_AUTOSCALEVMGROUP_ENABLE = "AUTOSCALEVMGROUP.ENABLE"; - public static final String EVENT_AUTOSCALEVMGROUP_DISABLE = "AUTOSCALEVMGROUP.DIABLE"; + public static final String EVENT_AUTOSCALEVMGROUP_DISABLE = "AUTOSCALEVMGROUP.DISABLE"; } diff --git a/api/src/com/cloud/network/VpcVirtualNetworkApplianceService.java b/api/src/com/cloud/network/VpcVirtualNetworkApplianceService.java index 3123cc2432b..eaaecf1a3c0 100644 --- a/api/src/com/cloud/network/VpcVirtualNetworkApplianceService.java +++ b/api/src/com/cloud/network/VpcVirtualNetworkApplianceService.java @@ -20,7 +20,7 @@ import com.cloud.network.router.VirtualRouter; /** * @author Alena Prokharchyk */ -public interface VpcVirtualNetworkApplianceService { +public interface VpcVirtualNetworkApplianceService extends VirtualNetworkApplianceService{ /** * @param router diff --git a/api/src/com/cloud/network/element/NetworkElement.java b/api/src/com/cloud/network/element/NetworkElement.java index c6845d3d346..e93962a0ebc 100644 --- a/api/src/com/cloud/network/element/NetworkElement.java +++ b/api/src/com/cloud/network/element/NetworkElement.java @@ -14,6 +14,7 @@ package com.cloud.network.element; import java.util.List; import java.util.Map; +import java.util.Set; import com.cloud.deploy.DeployDestination; import com.cloud.exception.ConcurrentOperationException; @@ -133,5 +134,5 @@ public interface NetworkElement extends Adapter { * @param services * @return true/false */ - boolean verifyServicesCombination(List services); + boolean verifyServicesCombination(Set services); } diff --git a/api/src/com/cloud/network/vpc/VpcService.java b/api/src/com/cloud/network/vpc/VpcService.java index 707443da106..dcb005225ac 100644 --- a/api/src/com/cloud/network/vpc/VpcService.java +++ b/api/src/com/cloud/network/vpc/VpcService.java @@ -77,7 +77,8 @@ public interface VpcService { * @return * @throws ResourceAllocationException TODO */ - public Vpc createVpc(long zoneId, long vpcOffId, long vpcOwnerId, String vpcName, String displayText, String cidr, String networkDomain) throws ResourceAllocationException; + public Vpc createVpc(long zoneId, long vpcOffId, long vpcOwnerId, String vpcName, String displayText, String cidr, + String networkDomain) throws ResourceAllocationException; /** * @param vpcId @@ -113,13 +114,14 @@ public interface VpcService { * @param listAll TODO * @param restartRequired TODO * @param tags TODO + * @param projectId TODO * @param vpc * @return */ public List listVpcs(Long id, String vpcName, String displayText, List supportedServicesStr, String cidr, Long vpcOffId, String state, String accountName, Long domainId, String keyword, Long startIndex, Long pageSizeVal, Long zoneId, Boolean isRecursive, Boolean listAll, - Boolean restartRequired, Map tags); + Boolean restartRequired, Map tags, Long projectId); /** * @param vpcId diff --git a/awsapi/src/com/cloud/bridge/persist/dao/CloudStackSvcOfferingDao.java b/awsapi/src/com/cloud/bridge/persist/dao/CloudStackSvcOfferingDao.java index 96c6ec721e7..922d5e1da42 100644 --- a/awsapi/src/com/cloud/bridge/persist/dao/CloudStackSvcOfferingDao.java +++ b/awsapi/src/com/cloud/bridge/persist/dao/CloudStackSvcOfferingDao.java @@ -15,10 +15,10 @@ */ package com.cloud.bridge.persist.dao; +import java.util.List; import org.apache.log4j.Logger; import com.cloud.bridge.persist.EntityDao; -import com.cloud.stack.models.CloudStackConfiguration; import com.cloud.stack.models.CloudStackServiceOffering; @@ -29,10 +29,9 @@ public class CloudStackSvcOfferingDao extends EntityDao getSvcOfferingByName( String name ){ + return queryEntities("from CloudStackServiceOffering where name=?", new Object[] {name}); + } public CloudStackServiceOffering getSvcOfferingById( String id ){ return queryEntity("from CloudStackServiceOffering where id=?", new Object[] {id}); diff --git a/awsapi/src/com/cloud/bridge/service/EC2SoapServiceImpl.java b/awsapi/src/com/cloud/bridge/service/EC2SoapServiceImpl.java index e239175f131..c1c6d8c6dd9 100644 --- a/awsapi/src/com/cloud/bridge/service/EC2SoapServiceImpl.java +++ b/awsapi/src/com/cloud/bridge/service/EC2SoapServiceImpl.java @@ -258,6 +258,10 @@ public class EC2SoapServiceImpl implements AmazonEC2SkeletonInterface { List resourceTypeList = new ArrayList(); if (items != null) { for( int i=0; i < items.length; i++ ) { + if (!items[i].getResourceId().contains(":") || items[i].getResourceId().split(":").length != 2) { + throw new EC2ServiceException( ClientError.InvalidResourceId_Format, + "Invalid Format. ResourceId format is resource-type:resource-uuid"); + } String resourceType = items[i].getResourceId().split(":")[0]; if (resourceTypeList.isEmpty()) resourceTypeList.add(resourceType); diff --git a/awsapi/src/com/cloud/bridge/service/core/ec2/EC2Engine.java b/awsapi/src/com/cloud/bridge/service/core/ec2/EC2Engine.java index fd54eb4d61f..449eaecee5a 100644 --- a/awsapi/src/com/cloud/bridge/service/core/ec2/EC2Engine.java +++ b/awsapi/src/com/cloud/bridge/service/core/ec2/EC2Engine.java @@ -1775,8 +1775,12 @@ public class EC2Engine { if (null == instanceType) instanceType = "m1.small"; CloudStackSvcOfferingDao dao = new CloudStackSvcOfferingDao(); - return dao.getSvcOfferingByName(instanceType); - + List svcOfferingList = dao.getSvcOfferingByName(instanceType); + for (CloudStackServiceOffering svcOffering : svcOfferingList) { + if (svcOffering.getRemoved() == null) + return svcOffering; + } + return null; } catch(Exception e) { logger.error( "Error while retrieving ServiceOffering information by name - ", e); throw new EC2ServiceException(ServerError.InternalError, e.getMessage()); diff --git a/awsapi/src/com/cloud/bridge/service/exception/EC2ServiceException.java b/awsapi/src/com/cloud/bridge/service/exception/EC2ServiceException.java index 8b26d61d40c..aca0c0a525c 100644 --- a/awsapi/src/com/cloud/bridge/service/exception/EC2ServiceException.java +++ b/awsapi/src/com/cloud/bridge/service/exception/EC2ServiceException.java @@ -77,6 +77,7 @@ public class EC2ServiceException extends RuntimeException { InvalidPermission_Malformed("Client.InvalidPermission.Malformed", 400), InvalidReservationID_Malformed("Client.InvalidReservationID.Malformed", 400), InvalidReservationID_NotFound("Client.InvalidReservationID.NotFound", 400), + InvalidResourceId_Format("Client.InvalidResourceId.Format", 400), InvalidSnapshotID_Malformed("Client.InvalidSnapshotID.Malformed", 400), InvalidSnapshot_NotFound("Client.InvalidSnapshot.NotFound", 400), InvalidUserID_Malformed("Client.InvalidUserID.Malformed", 400), diff --git a/awsapi/src/com/cloud/stack/models/CloudStackServiceOffering.hbm.xml b/awsapi/src/com/cloud/stack/models/CloudStackServiceOffering.hbm.xml index f3c5d4d17f3..e9c73b82434 100644 --- a/awsapi/src/com/cloud/stack/models/CloudStackServiceOffering.hbm.xml +++ b/awsapi/src/com/cloud/stack/models/CloudStackServiceOffering.hbm.xml @@ -11,6 +11,9 @@ + + + diff --git a/awsapi/src/com/cloud/stack/models/CloudStackServiceOffering.java b/awsapi/src/com/cloud/stack/models/CloudStackServiceOffering.java index 9dde25a553a..e3df1aaed8d 100644 --- a/awsapi/src/com/cloud/stack/models/CloudStackServiceOffering.java +++ b/awsapi/src/com/cloud/stack/models/CloudStackServiceOffering.java @@ -57,6 +57,8 @@ public class CloudStackServiceOffering { private String systemVmType; @SerializedName(ApiConstants.TAGS) private String tags; + @SerializedName(ApiConstants.REMOVED) + private String removed; /** * @@ -196,4 +198,14 @@ public class CloudStackServiceOffering { return tags; } + /** + * @return the removed + */ + public String getRemoved() { + return removed; + } + + public void setRemoved(String removed) { + this.removed = removed; + } } diff --git a/client/WEB-INF/classes/resources/messages.properties b/client/WEB-INF/classes/resources/messages.properties index f836e601c60..01088acec92 100644 --- a/client/WEB-INF/classes/resources/messages.properties +++ b/client/WEB-INF/classes/resources/messages.properties @@ -1,4 +1,6 @@ #new labels (begin) ********************************************************************************************** +label.tier.details=Tier details +label.edit.tags=Edit tags label.network.rate.megabytes=Network Rate (MB/s) label.action.enable.physical.network=Enable physical network label.action.disable.physical.network=Disable physical network diff --git a/console-proxy/src/com/cloud/consoleproxy/AjaxFIFOImageCache.java b/console-proxy/src/com/cloud/consoleproxy/AjaxFIFOImageCache.java index 49f528a7e30..bbf45cfefaf 100644 --- a/console-proxy/src/com/cloud/consoleproxy/AjaxFIFOImageCache.java +++ b/console-proxy/src/com/cloud/consoleproxy/AjaxFIFOImageCache.java @@ -40,9 +40,8 @@ public class AjaxFIFOImageCache { public synchronized int putImage(byte[] image) { while(cache.size() >= cacheSize) { - Integer keyToRemove = fifoQueue.remove(0); + Integer keyToRemove = fifoQueue.remove(0); cache.remove(keyToRemove); - if(s_logger.isTraceEnabled()) s_logger.trace("Remove image from cache, key: " + keyToRemove); } @@ -51,26 +50,30 @@ public class AjaxFIFOImageCache { if(s_logger.isTraceEnabled()) s_logger.trace("Add image to cache, key: " + key); - - cache.put(key, image); + + cache.put(key, image); fifoQueue.add(key); return key; } - public synchronized byte[] getImage(int key) { + public synchronized byte[] getImage(int key) { + if(cache.containsKey(key)) { if(s_logger.isTraceEnabled()) - s_logger.trace("Retrieve image from cache, key: " + key); - + s_logger.trace("Retrieve image from cache, key: " + key); + return cache.get(key); } - - if(s_logger.isTraceEnabled()) + s_logger.trace("Image is no long in cache, key: " + key); return null; } public synchronized int getNextKey() { return nextKey++; + } + + public synchronized int getKey() { + return nextKey; } } diff --git a/console-proxy/src/com/cloud/consoleproxy/ConsoleProxyAjaxImageHandler.java b/console-proxy/src/com/cloud/consoleproxy/ConsoleProxyAjaxImageHandler.java index bc01ed72474..a2736fb3df1 100644 --- a/console-proxy/src/com/cloud/consoleproxy/ConsoleProxyAjaxImageHandler.java +++ b/console-proxy/src/com/cloud/consoleproxy/ConsoleProxyAjaxImageHandler.java @@ -61,6 +61,9 @@ public class ConsoleProxyAjaxImageHandler implements HttpHandler { String tag = queryMap.get("tag"); String ticket = queryMap.get("ticket"); String keyStr = queryMap.get("key"); + String console_url = queryMap.get("consoleurl"); + String console_host_session = queryMap.get("sessionref"); + int key = 0; if(tag == null) @@ -90,8 +93,14 @@ public class ConsoleProxyAjaxImageHandler implements HttpHandler { param.setClientHostPassword(sid); param.setClientTag(tag); param.setTicket(ticket); - ConsoleProxyClient viewer = ConsoleProxy.getVncViewer(param); - byte[] img = viewer.getAjaxImageCache().getImage(key); + param.setClientTunnelUrl(console_url); + param.setClientTunnelSession(console_host_session); + + ConsoleProxyClient viewer = ConsoleProxy.getVncViewer(param); + AjaxFIFOImageCache imageCache =viewer.getAjaxImageCache(); + + byte[] img = imageCache.getImage(imageCache.getKey() - 1); + if(img != null) { Headers hds = t.getResponseHeaders(); hds.set("Content-Type", "image/jpeg"); diff --git a/core/src/com/cloud/agent/resource/virtualnetwork/VirtualRoutingResource.java b/core/src/com/cloud/agent/resource/virtualnetwork/VirtualRoutingResource.java index 936608c8b9e..252d80da81c 100755 --- a/core/src/com/cloud/agent/resource/virtualnetwork/VirtualRoutingResource.java +++ b/core/src/com/cloud/agent/resource/virtualnetwork/VirtualRoutingResource.java @@ -607,6 +607,8 @@ public class VirtualRoutingResource implements Manager { args = "-D"; args += " -r "; args += cmd.getPeerGatewayIp(); + args += " -n "; + args += cmd.getLocalGuestCidr(); args += " -N "; args += cmd.getPeerGuestCidrList(); } diff --git a/core/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java b/core/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java index c2a7d609d7c..4099531df9b 100755 --- a/core/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java +++ b/core/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java @@ -496,6 +496,10 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa } protected Answer execute(NetworkUsageCommand cmd) { + if ( cmd.isForVpc() ) { + return VPCNetworkUsage(cmd); + } + if (s_logger.isInfoEnabled()) { s_logger.info("Executing resource NetworkUsageCommand " + _gson.toJson(cmd)); } @@ -510,6 +514,69 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa return answer; } + protected NetworkUsageAnswer VPCNetworkUsage(NetworkUsageCommand cmd) { + String privateIp = cmd.getPrivateIP(); + String option = cmd.getOption(); + String publicIp = cmd.getGatewayIP(); + + + String args = "-l " + publicIp+ " "; + if (option.equals("get")) { + args += "-g"; + } else if (option.equals("create")) { + args += "-c"; + String vpcCIDR = cmd.getVpcCIDR(); + args += " -v " + vpcCIDR; + } else if (option.equals("reset")) { + args += "-r"; + } else if (option.equals("vpn")) { + args += "-n"; + } else if (option.equals("remove")) { + args += "-d"; + } else { + return new NetworkUsageAnswer(cmd, "success", 0L, 0L); + } + + try { + if (s_logger.isTraceEnabled()) { + s_logger.trace("Executing /opt/cloud/bin/vpc_netusage.sh " + args + " on DomR " + privateIp); + } + + VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); + + Pair resultPair = SshHelper.sshExecute(privateIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, "/opt/cloud/bin/vpc_netusage.sh " + args); + + if (!resultPair.first()) { + return null; + } + + String result = resultPair.second(); + + if (option.equals("get")) { + long[] stats = new long[2]; + if (result != null) { + String[] splitResult = result.split(":"); + int i = 0; + while (i < splitResult.length - 1) { + stats[0] += (new Long(splitResult[i++])).longValue(); + stats[1] += (new Long(splitResult[i++])).longValue(); + } + return new NetworkUsageAnswer(cmd, "success", stats[0], stats[1]); + } + } + if (result == null || result.isEmpty()) { + throw new Exception(" vpc network usage plugin call failed "); + } + return new NetworkUsageAnswer(cmd, "success", 0L, 0L); + + } catch (Throwable e) { + s_logger.error("Unable to execute NetworkUsage command on DomR (" + privateIp + "), domR may not be ready yet. failure due to " + + VmwareHelper.getExceptionMessage(e), e); + } + + return null; + } + private SetStaticRouteAnswer execute(SetStaticRouteCommand cmd) { if (s_logger.isInfoEnabled()) { s_logger.info("Executing resource SetStaticRouteCommand: " + _gson.toJson(cmd)); @@ -741,7 +808,95 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa return new SetStaticNatRulesAnswer(cmd, results, endResult); } + protected Answer VPCLoadBalancerConfig(final LoadBalancerConfigCommand cmd) { + VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); + File keyFile = mgr.getSystemVMKeyFile(); + + String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP); + String controlIp = getRouterSshControlIp(cmd); + + assert(controlIp != null); + + LoadBalancerConfigurator cfgtr = new HAProxyConfigurator(); + String[] config = cfgtr.generateConfiguration(cmd); + + String tmpCfgFilePath = "/etc/haproxy/haproxy.cfg.new"; + String tmpCfgFileContents = ""; + for (int i = 0; i < config.length; i++) { + tmpCfgFileContents += config[i]; + tmpCfgFileContents += "\n"; + } + + try { + SshHelper.scpTo(controlIp, DEFAULT_DOMR_SSHPORT, "root", keyFile, null, "/etc/haproxy/", tmpCfgFileContents.getBytes(), "haproxy.cfg.new", null); + + try { + String[][] rules = cfgtr.generateFwRules(cmd); + + String[] addRules = rules[LoadBalancerConfigurator.ADD]; + String[] removeRules = rules[LoadBalancerConfigurator.REMOVE]; + String[] statRules = rules[LoadBalancerConfigurator.STATS]; + + String args = ""; + String ip = cmd.getNic().getIp(); + args += " -i " + ip; + StringBuilder sb = new StringBuilder(); + if (addRules.length > 0) { + for (int i = 0; i < addRules.length; i++) { + sb.append(addRules[i]).append(','); + } + + args += " -a " + sb.toString(); + } + + sb = new StringBuilder(); + if (removeRules.length > 0) { + for (int i = 0; i < removeRules.length; i++) { + sb.append(removeRules[i]).append(','); + } + + args += " -d " + sb.toString(); + } + + sb = new StringBuilder(); + if (statRules.length > 0) { + for (int i = 0; i < statRules.length; i++) { + sb.append(statRules[i]).append(','); + } + + args += " -s " + sb.toString(); + } + + // Invoke the command + Pair result = SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, "/opt/cloud/bin/vpc_loadbalancer.sh " + args); + + if (!result.first()) { + String msg = "LoadBalancerConfigCommand on domain router " + routerIp + " failed. message: " + result.second(); + s_logger.error(msg); + + return new Answer(cmd, false, msg); + } + + if (s_logger.isInfoEnabled()) { + s_logger.info("VPCLoadBalancerConfigCommand on domain router " + routerIp + " completed"); + } + } finally { + SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", mgr.getSystemVMKeyFile(), null, "rm " + tmpCfgFilePath); + } + return new Answer(cmd); + } catch (Throwable e) { + s_logger.error("Unexpected exception: " + e.toString(), e); + return new Answer(cmd, false, "VPCLoadBalancerConfigCommand failed due to " + VmwareHelper.getExceptionMessage(e)); + } + + + } + protected Answer execute(final LoadBalancerConfigCommand cmd) { + if ( cmd.getVpcId() != null ) { + return VPCLoadBalancerConfig(cmd); + } + VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME); File keyFile = mgr.getSystemVMKeyFile(); @@ -869,7 +1024,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa // eth0:xx.xx.xx.xx // // list IP with eth devices - // ifconfig ethx |grep -B1 "inet addr" | awk '{ if ( $1 == "inet" ) { print $2 } else if ( $2 == "Link" ) { printf "%s:" ,$1 } }' + // ifconfig ethx |grep -B1 "inet addr" | awk '{ if ( $1 == "inet" ) { print $2 } else if ( $2 == "Link" ) { printf "%s:" ,$1 } }' // | awk -F: '{ print $1 ": " $3 }' // // @@ -1186,6 +1341,8 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa args += " -D"; args += " -r "; args += cmd.getPeerGatewayIp(); + args += " -n "; + args += cmd.getLocalGuestCidr(); args += " -N "; args += cmd.getPeerGuestCidrList(); } diff --git a/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java b/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java index 9ba5081981a..db50d1ee1ae 100644 --- a/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java +++ b/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java @@ -7406,6 +7406,8 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe args += " -D"; args += " -r "; args += cmd.getPeerGatewayIp(); + args += " -n "; + args += cmd.getLocalGuestCidr(); args += " -N "; args += cmd.getPeerGuestCidrList(); } diff --git a/core/src/com/cloud/hypervisor/xen/resource/XenServer56Resource.java b/core/src/com/cloud/hypervisor/xen/resource/XenServer56Resource.java index 0bc18500b3c..bf486376f2a 100644 --- a/core/src/com/cloud/hypervisor/xen/resource/XenServer56Resource.java +++ b/core/src/com/cloud/hypervisor/xen/resource/XenServer56Resource.java @@ -180,6 +180,10 @@ public class XenServer56Resource extends CitrixResourceBase { args += " -v " + vpcCIDR; } else if (option.equals("reset")) { args += "-r"; + } else if (option.equals("vpn")) { + args += "-n"; + } else if (option.equals("remove")) { + args += "-d"; } else { return new NetworkUsageAnswer(cmd, "success", 0L, 0L); } diff --git a/core/src/com/cloud/network/resource/NetscalerResource.java b/core/src/com/cloud/network/resource/NetscalerResource.java index c6448d13c88..f8ae5247349 100644 --- a/core/src/com/cloud/network/resource/NetscalerResource.java +++ b/core/src/com/cloud/network/resource/NetscalerResource.java @@ -42,8 +42,8 @@ import com.citrix.netscaler.nitro.resource.config.network.vlan_nsip_binding; import com.citrix.netscaler.nitro.resource.config.ns.nsconfig; import com.citrix.netscaler.nitro.resource.config.ns.nshardware; import com.citrix.netscaler.nitro.resource.config.ns.nsip; -import com.citrix.netscaler.nitro.resource.config.timer.timerpolicy; -import com.citrix.netscaler.nitro.resource.config.timer.timertrigger_timerpolicy_binding; +import com.citrix.netscaler.nitro.resource.config.ns.nstimer; +import com.citrix.netscaler.nitro.resource.config.ns.nstimer_autoscalepolicy_binding; import com.citrix.netscaler.nitro.resource.config.autoscale.*; import com.citrix.netscaler.nitro.resource.stat.lb.lbvserver_stats; import com.citrix.netscaler.nitro.service.nitro_service; @@ -238,7 +238,7 @@ public class NetscalerResource implements ServerResource { if (!_isSdx) { _netscalerService = new com.citrix.netscaler.nitro.service.nitro_service(_ip, "https"); _netscalerService.set_credential(_username, _password); - // _netscalerService.set_timeout(_timeout); + _netscalerService.set_timeout(_timeout); apiCallResult = _netscalerService.login(); if (apiCallResult.errorcode != 0) { throw new ExecutionException ("Failed to log in to Netscaler device at " + _ip + " due to error " + apiCallResult.errorcode + " and message " + apiCallResult.message); @@ -701,7 +701,7 @@ public class NetscalerResource implements ServerResource { try { nitro_service _netscalerService = new nitro_service(cmd.getLoadBalancerIP(), "https"); _netscalerService.set_credential(username, password); - // _netscalerService.set_timeout(_timeout); + _netscalerService.set_timeout(_timeout); apiCallResult = _netscalerService.login(); if (apiCallResult.errorcode == 0) { nsServiceUp = true; @@ -1367,15 +1367,15 @@ public class NetscalerResource implements ServerResource { boolean vserverExisis = false; lbvserver vserver = getVirtualServerIfExisits(virtualServerName); - if (vserver == null) { - vserver = new lbvserver(); - } else { + if (vserver != null) { if (!vserver.get_servicetype().equalsIgnoreCase(protocol)) { throw new ExecutionException("Can not update virtual server:" + virtualServerName + " as current protocol:" + vserver.get_servicetype() + " of virtual server is different from the " + " intended protocol:" + protocol); } vserverExisis = true; } + // Use new vserver always for configuration + vserver = new lbvserver(); vserver.set_name(virtualServerName); vserver.set_ipv46(publicIp); vserver.set_port(publicPort); @@ -1419,16 +1419,8 @@ public class NetscalerResource implements ServerResource { // set session persistence timeout vserver.set_timeout(timeout); } else { - if (vserver.get_persistencetype() != null) { - // delete the LB stickyness policy - vserver.set_persistencetype("NONE"); - } - } - if(vmGroupTO != null) { - vserver.set_mysqlcharacterset(null); - vserver.set_mysqlprotocolversion(null); - vserver.set_mysqlservercapabilities(null); - vserver.set_mysqlserverversion(null); + // delete the LB stickyness policy + vserver.set_persistencetype("NONE"); } @@ -1557,7 +1549,9 @@ public class NetscalerResource implements ServerResource { String nsVirtualServerName = generateNSVirtualServerName(srcIp, srcPort); String serviceGroupName = generateAutoScaleServiceGroupName(srcIp, srcPort); - disableAutoScaleConfig(loadBalancerTO, false); + if(loadBalancerTO.getAutoScaleVmGroupTO().getState().equals("enabled")) { + disableAutoScaleConfig(loadBalancerTO, false); + } if(isServiceGroupBoundToVirtualServer(nsVirtualServerName, serviceGroupName)) { // UnBind autoscale service group @@ -1648,7 +1642,7 @@ public class NetscalerResource implements ServerResource { } // Add Timer - com.citrix.netscaler.nitro.resource.config.timer.timertrigger timer = new com.citrix.netscaler.nitro.resource.config.timer.timertrigger(); + nstimer timer = new nstimer(); try { timer.set_name(timerName); timer.set_interval(interval); @@ -1945,7 +1939,7 @@ public class NetscalerResource implements ServerResource { } // Delete Timer - com.citrix.netscaler.nitro.resource.config.timer.timertrigger timer = new com.citrix.netscaler.nitro.resource.config.timer.timertrigger(); + nstimer timer = new nstimer(); try { timer.set_name(timerName); timer.delete(_netscalerService, timer); @@ -2017,7 +2011,7 @@ public class NetscalerResource implements ServerResource { // Adding a autoscale policy // add timer policy lb_policy_scaleUp_cpu_mem -rule - (SYS.CUR_VSERVER.METRIC_TABLE(cpu).AVG_VAL.GT(80)- // -action lb_scaleUpAction - timerpolicy timerPolicy = new timerpolicy(); + autoscalepolicy timerPolicy = new autoscalepolicy(); try { timerPolicy.set_name(policyName); timerPolicy.set_action(action); @@ -2033,7 +2027,7 @@ public class NetscalerResource implements ServerResource { // bind timer trigger lb_astimer -policyName lb_policy_scaleUp -vserver lb -priority 1 -samplesize 5 // TODO: later bind to lbvserver. bind timer trigger lb_astimer -policyName lb_policy_scaleUp -vserver lb -priority 1 -samplesize 5 // -thresholdsize 5 - timertrigger_timerpolicy_binding timer_policy_binding = new timertrigger_timerpolicy_binding(); + nstimer_autoscalepolicy_binding timer_policy_binding = new nstimer_autoscalepolicy_binding(); int sampleSize = duration/interval; try { timer_policy_binding.set_name(timerName); @@ -2053,7 +2047,7 @@ public class NetscalerResource implements ServerResource { private void removeAutoScalePolicy(String timerName, String policyName, boolean isCleanUp) throws Exception { // unbind timer policy // unbbind timer trigger lb_astimer -policyName lb_policy_scaleUp - com.citrix.netscaler.nitro.resource.config.timer.timertrigger_timerpolicy_binding timer_policy_binding = new com.citrix.netscaler.nitro.resource.config.timer.timertrigger_timerpolicy_binding(); + nstimer_autoscalepolicy_binding timer_policy_binding = new nstimer_autoscalepolicy_binding(); try { timer_policy_binding.set_name(timerName); timer_policy_binding.set_policyname(policyName); @@ -2066,7 +2060,7 @@ public class NetscalerResource implements ServerResource { // Removing Timer policy // rm timer policy lb_policy_scaleUp_cpu_mem - com.citrix.netscaler.nitro.resource.config.timer.timerpolicy timerPolicy = new com.citrix.netscaler.nitro.resource.config.timer.timerpolicy(); + autoscalepolicy timerPolicy = new autoscalepolicy(); try { timerPolicy.set_name(policyName); timerPolicy.delete(_netscalerService, timerPolicy); @@ -2278,8 +2272,4 @@ public class NetscalerResource implements ServerResource { public void disconnected() { return; } - - - - } diff --git a/deps/cloud-netscaler.jar b/deps/cloud-netscaler.jar index 3097b787885..475ff638440 100755 Binary files a/deps/cloud-netscaler.jar and b/deps/cloud-netscaler.jar differ diff --git a/patches/systemvm/debian/config/opt/cloud/bin/ipsectunnel.sh b/patches/systemvm/debian/config/opt/cloud/bin/ipsectunnel.sh index 1bc20025d8c..196221295dc 100755 --- a/patches/systemvm/debian/config/opt/cloud/bin/ipsectunnel.sh +++ b/patches/systemvm/debian/config/opt/cloud/bin/ipsectunnel.sh @@ -21,6 +21,7 @@ fi vpnconfdir="/etc/ipsec.d" vpnoutmark="0x525" +vpninmark="0x524" usage() { printf "Usage: %s: (-A|-D) -l -n -g -r -N -e -i -t -T -s -d \n" $(basename $0) >&2 @@ -55,6 +56,8 @@ enable_iptables_subnets() { do sudo iptables -A FORWARD -t mangle -s $leftnet -d $net -j MARK --set-mark $vpnoutmark sudo iptables -A OUTPUT -t mangle -s $leftnet -d $net -j MARK --set-mark $vpnoutmark + sudo iptables -A FORWARD -t mangle -s $net -d $leftnet -j MARK --set-mark $vpninmark + sudo iptables -A INPUT -t mangle -s $net -d $leftnet -j MARK --set-mark $vpninmark done return 0 } @@ -64,6 +67,7 @@ check_and_enable_iptables() { if [ $? -ne 0 ] then sudo iptables -A INPUT -i $outIf -p udp -m udp --dport 500 -j ACCEPT + sudo iptables -A INPUT -i $outIf -p udp -m udp --dport 4500 -j ACCEPT # Prevent NAT on "marked" VPN traffic, so need to be the first one on POSTROUTING chain sudo iptables -t nat -I POSTROUTING -t nat -o $outIf -m mark --mark $vpnoutmark -j ACCEPT fi @@ -75,6 +79,8 @@ disable_iptables_subnets() { do sudo iptables -D FORWARD -t mangle -s $leftnet -d $net -j MARK --set-mark $vpnoutmark sudo iptables -D OUTPUT -t mangle -s $leftnet -d $net -j MARK --set-mark $vpnoutmark + sudo iptables -D FORWARD -t mangle -s $net -d $leftnet -j MARK --set-mark $vpninmark + sudo iptables -D INPUT -t mangle -s $net -d $leftnet -j MARK --set-mark $vpninmark done return 0 } @@ -85,6 +91,7 @@ check_and_disable_iptables() { then #Nobody else use s2s vpn now, so delete the iptables rules sudo iptables -D INPUT -i $outIf -p udp -m udp --dport 500 -j ACCEPT + sudo iptables -D INPUT -i $outIf -p udp -m udp --dport 4500 -j ACCEPT sudo iptables -t nat -D POSTROUTING -t nat -o $outIf -m mark --mark $vpnoutmark -j ACCEPT fi return 0 @@ -142,7 +149,7 @@ ipsec_tunnel_add() { sudo echo " esp=$esppolicy" >> $vpnconffile && sudo echo " salifetime=${esplifetime}s" >> $vpnconffile && sudo echo " pfs=$pfs" >> $vpnconffile && - sudo echo " keyingtries=3" >> $vpnconffile && + sudo echo " keyingtries=2" >> $vpnconffile && sudo echo " auto=add" >> $vpnconffile && sudo echo "$leftpeer $rightpeer: PSK \"$secret\"" > $vpnsecretsfile && sudo chmod 0400 $vpnsecretsfile @@ -162,8 +169,8 @@ ipsec_tunnel_add() { logger -t cloud "$(basename $0): done ipsec tunnel entry for right peer=$rightpeer right networks=$rightnets" - #20 seconds for checking if it's ready - for i in {1..4} + #5 seconds for checking if it's ready + for i in {1..5} do logger -t cloud "$(basename $0): checking connection status..." /opt/cloud/bin/checks2svpn.sh $rightpeer @@ -172,7 +179,7 @@ ipsec_tunnel_add() { then break fi - sleep 5 + sleep 1 done if [ $result -eq 0 ] then diff --git a/patches/systemvm/debian/config/opt/cloud/bin/vpc_netusage.sh b/patches/systemvm/debian/config/opt/cloud/bin/vpc_netusage.sh index 8ec44a1b90e..b026eb4c2fe 100755 --- a/patches/systemvm/debian/config/opt/cloud/bin/vpc_netusage.sh +++ b/patches/systemvm/debian/config/opt/cloud/bin/vpc_netusage.sh @@ -15,6 +15,8 @@ source /root/func.sh source /opt/cloud/bin/vpc_func.sh +vpnoutmark="0x525" +vpninmark="0x524" lock="biglock" locked=$(getLockFile $lock) if [ "$locked" != "1" ] @@ -23,27 +25,62 @@ then fi usage() { - printf "Usage: %s -[c|g|r] [-[a|d] ]\n" $(basename $0) >&2 + printf "Usage: %s -[c|g|r|n|d] [-l ] [-v ] \n" $(basename $0) >&2 } create_usage_rules () { - iptables -N NETWORK_STATS_$ethDev > /dev/null - iptables -I FORWARD -j NETWORK_STATS_$ethDev > /dev/null - iptables-save|grep "NETWORK_STATS_$ethDev -i $ethDev" > /dev/null + iptables-save|grep "NETWORK_STATS_$ethDev" > /dev/null if [ $? -gt 0 ] then - iptables -A NETWORK_STATS_$ethDev -i $ethDev -d $vcidr > /dev/null - fi - iptables-save|grep "NETWORK_STATS_$ethDev -o $ethDev" > /dev/null + iptables -N NETWORK_STATS_$ethDev > /dev/null; + iptables -I FORWARD -j NETWORK_STATS_$ethDev > /dev/null; + iptables -A NETWORK_STATS_$ethDev -i $ethDev -d $vcidr > /dev/null; + iptables -A NETWORK_STATS_$ethDev -o $ethDev -s $vcidr > /dev/null; + fi + return $? +} + +create_vpn_usage_rules () { + iptables-save|grep "VPN_STATS_$ethDev" > /dev/null if [ $? -gt 0 ] then - iptables -A NETWORK_STATS_$ethDev -o $ethDev -s $vcidr > /dev/null + iptables -N VPN_STATS_$ethDev > /dev/null; + iptables -I FORWARD -j VPN_STATS_$ethDev > /dev/null; + iptables -A VPN_STATS_$ethDev -i $ethDev -m mark --mark $vpninmark > /dev/null; + iptables -A VPN_STATS_$ethDev -o $ethDev -m mark --mark $vpnoutmark > /dev/null; fi return $? } +remove_usage_rules () { + echo $ethDev >> /root/removedVifs + return $? +} + get_usage () { iptables -L NETWORK_STATS_$ethDev -n -v -x | awk '$1 ~ /^[0-9]+$/ { printf "%s:", $2}'; > /dev/null + if [ -f /root/removedVifs ] + then + var=`cat /root/removedVifs` + # loop through vifs to be cleared + for i in $var; do + # Make sure vif doesn't exist + if [ ! -f /sys/class/net/$i ] + then + # flush rules and remove chain + iptables -F NETWORK_STATS_$i > /dev/null; + iptables -X NETWORK_STATS_$i > /dev/null; + iptables -F VPN_STATS_$i > /dev/null; + iptables -X VPN_STATS_$i > /dev/null; + fi + done + rm /root/removedVifs + fi + return 1 +} + +get_vpn_usage () { + iptables -L VPN_STATS_$ethDev -n -v -x | awk '$1 ~ /^[0-9]+$/ { printf "%s:", $2}'; > /dev/null if [ $? -gt 0 ] then printf $? @@ -51,6 +88,8 @@ get_usage () { fi } + + reset_usage () { iptables -Z NETWORK_STATS_$ethDev > /dev/null if [ $? -gt 0 -a $? -ne 2 ] @@ -65,9 +104,11 @@ cflag= gflag= rflag= lflag= +vflag= +nflag= +dflag= - -while getopts 'cgrl:v:' OPTION +while getopts 'cgndrl:v:' OPTION do case $OPTION in c) cflag=1 @@ -82,6 +123,10 @@ do v) vflag=1 vcidr="$OPTARG" ;; + n) nflag=1 + ;; + d) dflag=1 + ;; i) #Do nothing, since it's parameter for host script ;; ?) usage @@ -93,8 +138,12 @@ done ethDev=$(getEthByIp $publicIp) if [ "$cflag" == "1" ] then - create_usage_rules - unlock_exit 0 $lock $locked + if [ "$ethDev" != "" ] + then + create_usage_rules + create_vpn_usage_rules + unlock_exit 0 $lock $locked + fi fi if [ "$gflag" == "1" ] @@ -103,6 +152,18 @@ then unlock_exit $? $lock $locked fi +if [ "$nflag" == "1" ] +then + get_vpn_usage + unlock_exit $? $lock $locked +fi + +if [ "$dflag" == "1" ] +then + remove_usage_rules + unlock_exit 0 $lock $locked +fi + if [ "$rflag" == "1" ] then reset_usage diff --git a/server/src/com/cloud/api/ApiResponseHelper.java b/server/src/com/cloud/api/ApiResponseHelper.java index 81b0901d86a..5fdc3dd2be1 100755 --- a/server/src/com/cloud/api/ApiResponseHelper.java +++ b/server/src/com/cloud/api/ApiResponseHelper.java @@ -469,6 +469,7 @@ public class ApiResponseHelper implements ResponseGenerator { } diskOfferingResponse.setTags(offering.getTags()); diskOfferingResponse.setCustomized(offering.isCustomized()); + diskOfferingResponse.setStorageType(offering.getUseLocalStorage() ? ServiceOffering.StorageType.local.toString() : ServiceOffering.StorageType.shared.toString()); diskOfferingResponse.setObjectName("diskoffering"); return diskOfferingResponse; } @@ -1123,24 +1124,6 @@ public class ApiResponseHelper implements ResponseGenerator { populateOwner(volResponse, volume); - String storageType; - try { - if (volume.getPoolId() == null) { - if (volume.getState() == Volume.State.Allocated || volume.getState() == Volume.State.UploadOp) { - /* set it as shared, so the UI can attach it to VM */ - storageType = "shared"; - } else { - storageType = "unknown"; - } - } else { - storageType = ApiDBUtils.volumeIsOnSharedStorage(volume.getId()) ? ServiceOffering.StorageType.shared.toString() : ServiceOffering.StorageType.local.toString(); - } - } catch (InvalidParameterValueException e) { - s_logger.error(e.getMessage(), e); - throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Volume " + volume.getName() + " does not have a valid ID"); - } - - volResponse.setStorageType(storageType); if (volume.getVolumeType().equals(Volume.Type.ROOT)) { volResponse.setServiceOfferingId(volume.getDiskOfferingId()); } else { @@ -1155,6 +1138,7 @@ public class ApiResponseHelper implements ResponseGenerator { volResponse.setDiskOfferingName(diskOffering.getName()); volResponse.setDiskOfferingDisplayText(diskOffering.getDisplayText()); } + volResponse.setStorageType(diskOffering.getUseLocalStorage() ? ServiceOffering.StorageType.local.toString() : ServiceOffering.StorageType.shared.toString()); Long poolId = volume.getPoolId(); String poolName = (poolId == null) ? "none" : ApiDBUtils.findStoragePoolById(poolId).getName(); diff --git a/server/src/com/cloud/baremetal/BareMetalPingServiceImpl.java b/server/src/com/cloud/baremetal/BareMetalPingServiceImpl.java index 894c493baff..9519a612e8a 100755 --- a/server/src/com/cloud/baremetal/BareMetalPingServiceImpl.java +++ b/server/src/com/cloud/baremetal/BareMetalPingServiceImpl.java @@ -63,7 +63,7 @@ public class BareMetalPingServiceImpl extends BareMetalPxeServiceBase implements List idList = new ArrayList(); idList.add(new IdentityProxy("pod", podId, "podId")); idList.add(new IdentityProxy(zone, zoneId, "zoneId")); - InvalidParameterValueException ex = new InvalidParameterValueException("Already had a PXE server in Pod with specified podId and zone with specified zoneId", idList); + throw new InvalidParameterValueException("Already had a PXE server in Pod with specified podId and zone with specified zoneId", idList); } diff --git a/server/src/com/cloud/configuration/Config.java b/server/src/com/cloud/configuration/Config.java index 5a8a24cc524..ccf18fed0ee 100755 --- a/server/src/com/cloud/configuration/Config.java +++ b/server/src/com/cloud/configuration/Config.java @@ -21,7 +21,7 @@ import com.cloud.consoleproxy.ConsoleProxyManager; import com.cloud.ha.HighAvailabilityManager; import com.cloud.hypervisor.Hypervisor.HypervisorType; import com.cloud.network.NetworkManager; -import com.cloud.network.router.VirtualNetworkApplianceManager; +import com.cloud.network.router.VpcVirtualNetworkApplianceManager; import com.cloud.server.ManagementServer; import com.cloud.storage.StorageManager; import com.cloud.storage.allocator.StoragePoolAllocator; @@ -154,7 +154,7 @@ public enum Config { PingTimeout("Advanced", AgentManager.class, Float.class, "ping.timeout", "2.5", "Multiplier to ping.interval before announcing an agent has timed out", null), ClusterDeltaSyncInterval("Advanced", AgentManager.class, Integer.class, "sync.interval", "60", "Cluster Delta sync interval in seconds", null), Port("Advanced", AgentManager.class, Integer.class, "port", "8250", "Port to listen on for agent connection.", null), - RouterCpuMHz("Advanced", NetworkManager.class, Integer.class, "router.cpu.mhz", String.valueOf(VirtualNetworkApplianceManager.DEFAULT_ROUTER_CPU_MHZ), "Default CPU speed (MHz) for router VM.", null), + RouterCpuMHz("Advanced", NetworkManager.class, Integer.class, "router.cpu.mhz", String.valueOf(VpcVirtualNetworkApplianceManager.DEFAULT_ROUTER_CPU_MHZ), "Default CPU speed (MHz) for router VM.", null), RestartRetryInterval("Advanced", HighAvailabilityManager.class, Integer.class, "restart.retry.interval", "600", "Time (in seconds) between retries to restart a vm", null), RouterStatsInterval("Advanced", NetworkManager.class, Integer.class, "router.stats.interval", "300", "Interval (in seconds) to report router statistics.", null), ExternalNetworkStatsInterval("Advanced", NetworkManager.class, Integer.class, "external.network.stats.interval", "300", "Interval (in seconds) to report external network statistics.", null), diff --git a/server/src/com/cloud/configuration/DefaultComponentLibrary.java b/server/src/com/cloud/configuration/DefaultComponentLibrary.java index 72dcf986b2c..7d40f46e515 100755 --- a/server/src/com/cloud/configuration/DefaultComponentLibrary.java +++ b/server/src/com/cloud/configuration/DefaultComponentLibrary.java @@ -122,7 +122,6 @@ import com.cloud.network.lb.dao.ElasticLbVmMapDaoImpl; import com.cloud.network.ovs.OvsTunnelManagerImpl; import com.cloud.network.ovs.dao.OvsTunnelInterfaceDaoImpl; import com.cloud.network.ovs.dao.OvsTunnelNetworkDaoImpl; -import com.cloud.network.router.VirtualNetworkApplianceManagerImpl; import com.cloud.network.router.VpcVirtualNetworkApplianceManagerImpl; import com.cloud.network.rules.RulesManagerImpl; import com.cloud.network.rules.dao.PortForwardingRulesDaoImpl; @@ -401,7 +400,6 @@ public class DefaultComponentLibrary extends ComponentLibraryBase implements Com addManager("Snapshot Manager", SnapshotManagerImpl.class); addManager("SnapshotScheduler", SnapshotSchedulerImpl.class); addManager("SecurityGroupManager", SecurityGroupManagerImpl2.class); - addManager("DomainRouterManager", VirtualNetworkApplianceManagerImpl.class); addManager("EntityManager", EntityManagerImpl.class); addManager("LoadBalancingRulesManager", LoadBalancingRulesManagerImpl.class); addManager("AutoScaleManager", AutoScaleManagerImpl.class); diff --git a/server/src/com/cloud/ha/UserVmDomRInvestigator.java b/server/src/com/cloud/ha/UserVmDomRInvestigator.java index 83fa0a64e98..92f6d4921ad 100644 --- a/server/src/com/cloud/ha/UserVmDomRInvestigator.java +++ b/server/src/com/cloud/ha/UserVmDomRInvestigator.java @@ -29,8 +29,8 @@ import com.cloud.host.Status; import com.cloud.hypervisor.Hypervisor.HypervisorType; import com.cloud.network.NetworkManager; import com.cloud.network.Networks.TrafficType; -import com.cloud.network.router.VirtualNetworkApplianceManager; import com.cloud.network.router.VirtualRouter; +import com.cloud.network.router.VpcVirtualNetworkApplianceManager; import com.cloud.utils.component.Inject; import com.cloud.vm.Nic; import com.cloud.vm.UserVmVO; @@ -46,7 +46,7 @@ public class UserVmDomRInvestigator extends AbstractInvestigatorImpl { @Inject private final UserVmDao _userVmDao = null; @Inject private final AgentManager _agentMgr = null; @Inject private final NetworkManager _networkMgr = null; - @Inject private final VirtualNetworkApplianceManager _vnaMgr = null; + @Inject private final VpcVirtualNetworkApplianceManager _vnaMgr = null; @Override public Boolean isVmAlive(VMInstanceVO vm, HostVO host) { diff --git a/server/src/com/cloud/hypervisor/vmware/VmwareManagerImpl.java b/server/src/com/cloud/hypervisor/vmware/VmwareManagerImpl.java index 8ff7633dd3a..3d096a89a81 100755 --- a/server/src/com/cloud/hypervisor/vmware/VmwareManagerImpl.java +++ b/server/src/com/cloud/hypervisor/vmware/VmwareManagerImpl.java @@ -70,7 +70,6 @@ import com.cloud.hypervisor.vmware.util.VmwareContext; import com.cloud.network.CiscoNexusVSMDeviceVO; import com.cloud.network.NetworkManager; import com.cloud.network.dao.CiscoNexusVSMDeviceDao; -import com.cloud.network.router.VirtualNetworkApplianceManager; import com.cloud.org.Cluster.ClusterType; import com.cloud.secstorage.CommandExecLogDao; import com.cloud.serializer.GsonHelper; @@ -92,7 +91,6 @@ import com.cloud.vm.DomainRouterVO; import com.google.gson.Gson; import com.vmware.apputils.vim25.ServiceUtil; import com.vmware.vim25.HostConnectSpec; -import com.vmware.vim25.HostPortGroupSpec; import com.vmware.vim25.ManagedObjectReference; @Local(value = {VmwareManager.class}) @@ -117,7 +115,6 @@ public class VmwareManagerImpl implements VmwareManager, VmwareStorageMount, Lis @Inject CommandExecLogDao _cmdExecLogDao; @Inject ClusterManager _clusterMgr; @Inject CheckPointManager _checkPointMgr; - @Inject VirtualNetworkApplianceManager _routerMgr; @Inject SecondaryStorageVmManager _ssvmMgr; @Inject CiscoNexusVSMDeviceDao _nexusDao; @Inject ClusterVSMMapDao _vsmMapDao; diff --git a/server/src/com/cloud/network/NetworkManagerImpl.java b/server/src/com/cloud/network/NetworkManagerImpl.java index 02745b7abc5..9d072b1f39c 100755 --- a/server/src/com/cloud/network/NetworkManagerImpl.java +++ b/server/src/com/cloud/network/NetworkManagerImpl.java @@ -4331,27 +4331,25 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag @Override public boolean networkIsConfiguredForExternalNetworking(long zoneId, long networkId) { - boolean netscalerInNetwork = isProviderForNetwork(Network.Provider.Netscaler, networkId); - boolean juniperInNetwork = isProviderForNetwork(Network.Provider.JuniperSRX, networkId); - boolean f5InNetwork = isProviderForNetwork(Network.Provider.F5BigIp, networkId); - - if (netscalerInNetwork || juniperInNetwork || f5InNetwork) { - return true; - } else { - return false; + List networkProviders = getNetworkProviders(networkId); + for(Provider provider : networkProviders){ + if(provider.isExternal()){ + return true; + } } + return false; } + public boolean networkOfferingIsConfiguredForExternalNetworking(long networkOfferingId) { - boolean netscalerInNetworkOffering = isProviderForNetworkOffering(Network.Provider.Netscaler, networkOfferingId); - boolean juniperInNetworkOffering = isProviderForNetworkOffering(Network.Provider.JuniperSRX, networkOfferingId); - boolean f5InNetworkOffering = isProviderForNetworkOffering(Network.Provider.F5BigIp, networkOfferingId); - - if (netscalerInNetworkOffering || juniperInNetworkOffering || f5InNetworkOffering) { - return true; - } else { - return false; + List networkOffProviders = getNtwkOffDistinctProviders(networkOfferingId); + for(Provider provider : networkOffProviders){ + if(provider.isExternal()){ + return true; + } } + return false; + } @Override @@ -6748,14 +6746,6 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag l.add(service); } - for (String provider : providerSvcs.keySet()) { - NetworkElement element = getElementImplementingProvider(provider); - List services = providerSvcs.get(provider); - if (!element.verifyServicesCombination(services)) { - throw new UnsupportedServiceException("Provider " + provider + " doesn't support services combination: " + services); - } - } - return svcProviders; } @@ -6780,12 +6770,14 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag private List getNetworkProviders(long networkId) { List providerNames = _ntwkSrvcDao.getDistinctProviders(networkId); - List providers = new ArrayList(); + Map providers = new HashMap(); for (String providerName : providerNames) { - providers.add(Network.Provider.getProvider(providerName)); + if(!providers.containsKey(providerName)){ + providers.put(providerName, Network.Provider.getProvider(providerName)); + } } - return providers; + return new ArrayList(providers.values()); } @Override @@ -6852,11 +6844,16 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag + provider.getName(), null); } } + List serviceList = new ArrayList(); for (Service service : enabledServices) { // check if the service is provided by this Provider if (!element.getCapabilities().containsKey(service)) { throw new UnsupportedServiceException(provider.getName() + " Provider cannot provide service " + service.getName()); } + serviceList.add(service.getName()); + } + if (!element.verifyServicesCombination(enabledServices)) { + throw new UnsupportedServiceException("Provider " + provider.getName() + " doesn't support services combination: " + serviceList); } } } @@ -7128,12 +7125,14 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag @Override public List getNtwkOffDistinctProviders(long networkId) { List providerNames = _ntwkOfferingSrvcDao.getDistinctProviders(networkId); - List providers = new ArrayList(); + Map providers = new HashMap(); for (String providerName : providerNames) { - providers.add(Network.Provider.getProvider(providerName)); + if(!providers.containsKey(providerName)){ + providers.put(providerName, Network.Provider.getProvider(providerName)); + } } - return providers; + return new ArrayList(providers.values()); } @Override diff --git a/server/src/com/cloud/network/NetworkVO.java b/server/src/com/cloud/network/NetworkVO.java index b1f1c0a8b41..90e2617323f 100644 --- a/server/src/com/cloud/network/NetworkVO.java +++ b/server/src/com/cloud/network/NetworkVO.java @@ -454,7 +454,7 @@ public class NetworkVO implements Network, Identity { @Override public String toString() { StringBuilder buf = new StringBuilder("Ntwk["); - buf.append(id).append("|").append(trafficType.toString()).append("|").append(networkOfferingId).append("]"); + buf.append(id).append("|").append(trafficType).append("|").append(networkOfferingId).append("]"); return buf.toString(); } diff --git a/server/src/com/cloud/network/SshKeysDistriMonitor.java b/server/src/com/cloud/network/SshKeysDistriMonitor.java index 25e0f2a78cb..cf2dfe39b52 100755 --- a/server/src/com/cloud/network/SshKeysDistriMonitor.java +++ b/server/src/com/cloud/network/SshKeysDistriMonitor.java @@ -12,9 +12,6 @@ // Automatically generated by addcopyright.py at 04/03/2012 package com.cloud.network; -import java.util.HashMap; -import java.util.Map; - import org.apache.log4j.Logger; import com.cloud.agent.AgentManager; @@ -34,8 +31,6 @@ import com.cloud.host.HostVO; import com.cloud.host.Status; import com.cloud.host.dao.HostDao; import com.cloud.hypervisor.Hypervisor.HypervisorType; -import com.cloud.network.router.VirtualNetworkApplianceManager; - public class SshKeysDistriMonitor implements Listener { diff --git a/server/src/com/cloud/network/as/AutoScaleManagerImpl.java b/server/src/com/cloud/network/as/AutoScaleManagerImpl.java index d0ea5c321b0..efff14e7d96 100644 --- a/server/src/com/cloud/network/as/AutoScaleManagerImpl.java +++ b/server/src/com/cloud/network/as/AutoScaleManagerImpl.java @@ -719,14 +719,15 @@ public class AutoScaleManagerImpl implements AutoScaleService, Manager { return vmGroup.getLoadBalancerId() != null; } - public boolean configureAutoScaleVmGroup(long vmGroupid) { + private boolean configureAutoScaleVmGroup(long vmGroupid) throws InvalidParameterValueException { AutoScaleVmGroup vmGroup = _autoScaleVmGroupDao.findById(vmGroupid); if (isLoadBalancerBasedAutoScaleVmGroup(vmGroup)) { try { return _lbRulesMgr.configureLbAutoScaleVmGroup(vmGroupid); - } catch (RuntimeException re) { - s_logger.warn("Exception during configureLbAutoScaleVmGrouop in lb rules manager", re); + } catch (Exception e) { + s_logger.warn("Exception during configureLbAutoScaleVmGroup in lb rules manager", e); + return false; } } diff --git a/server/src/com/cloud/network/element/BareMetalElement.java b/server/src/com/cloud/network/element/BareMetalElement.java index 1e56003d1f0..143af710e6b 100644 --- a/server/src/com/cloud/network/element/BareMetalElement.java +++ b/server/src/com/cloud/network/element/BareMetalElement.java @@ -14,6 +14,7 @@ package com.cloud.network.element; import java.util.List; import java.util.Map; +import java.util.Set; import javax.ejb.Local; @@ -118,7 +119,7 @@ public class BareMetalElement extends AdapterBase implements NetworkElement { } @Override - public boolean verifyServicesCombination(List services) { + public boolean verifyServicesCombination(Set services) { return true; } } diff --git a/server/src/com/cloud/network/element/CiscoNexusVSMElement.java b/server/src/com/cloud/network/element/CiscoNexusVSMElement.java index c57b6644fe1..557d6f3f0c0 100644 --- a/server/src/com/cloud/network/element/CiscoNexusVSMElement.java +++ b/server/src/com/cloud/network/element/CiscoNexusVSMElement.java @@ -15,6 +15,7 @@ package com.cloud.network.element; import java.util.List; import java.util.Map; import java.util.ArrayList; +import java.util.Set; import javax.ejb.Local; @@ -127,7 +128,7 @@ public class CiscoNexusVSMElement extends CiscoNexusVSMDeviceManagerImpl impleme } @Override - public boolean verifyServicesCombination(List services) { + public boolean verifyServicesCombination(Set services) { return true; } diff --git a/server/src/com/cloud/network/element/CloudZonesNetworkElement.java b/server/src/com/cloud/network/element/CloudZonesNetworkElement.java index 95e5297da2e..a6ffa8d9ba8 100644 --- a/server/src/com/cloud/network/element/CloudZonesNetworkElement.java +++ b/server/src/com/cloud/network/element/CloudZonesNetworkElement.java @@ -15,6 +15,7 @@ package com.cloud.network.element; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; import javax.ejb.Local; @@ -43,7 +44,6 @@ import com.cloud.network.NetworkManager; import com.cloud.network.Networks.TrafficType; import com.cloud.network.PhysicalNetworkServiceProvider; import com.cloud.network.dao.NetworkDao; -import com.cloud.network.router.VirtualNetworkApplianceManager; import com.cloud.offering.NetworkOffering; import com.cloud.service.dao.ServiceOfferingDao; import com.cloud.uservm.UserVm; @@ -70,8 +70,6 @@ public class CloudZonesNetworkElement extends AdapterBase implements NetworkElem @Inject NetworkManager _networkMgr; @Inject - VirtualNetworkApplianceManager _routerMgr; - @Inject UserVmManager _userVmMgr; @Inject UserVmDao _userVmDao; @@ -236,7 +234,7 @@ public class CloudZonesNetworkElement extends AdapterBase implements NetworkElem } @Override - public boolean verifyServicesCombination(List services) { + public boolean verifyServicesCombination(Set services) { return true; } diff --git a/server/src/com/cloud/network/element/ElasticLoadBalancerElement.java b/server/src/com/cloud/network/element/ElasticLoadBalancerElement.java index a7ed40df194..55dfe7d33a5 100644 --- a/server/src/com/cloud/network/element/ElasticLoadBalancerElement.java +++ b/server/src/com/cloud/network/element/ElasticLoadBalancerElement.java @@ -178,7 +178,7 @@ public class ElasticLoadBalancerElement extends AdapterBase implements LoadBalan } @Override - public boolean verifyServicesCombination(List services) { + public boolean verifyServicesCombination(Set services) { return true; } diff --git a/server/src/com/cloud/network/element/ExternalDhcpElement.java b/server/src/com/cloud/network/element/ExternalDhcpElement.java index 262aa8b02c4..89cfbc06b9c 100755 --- a/server/src/com/cloud/network/element/ExternalDhcpElement.java +++ b/server/src/com/cloud/network/element/ExternalDhcpElement.java @@ -15,6 +15,7 @@ package com.cloud.network.element; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; import javax.ejb.Local; @@ -142,7 +143,7 @@ public class ExternalDhcpElement extends AdapterBase implements NetworkElement, } @Override - public boolean verifyServicesCombination(List services) { + public boolean verifyServicesCombination(Set services) { return true; } } diff --git a/server/src/com/cloud/network/element/F5ExternalLoadBalancerElement.java b/server/src/com/cloud/network/element/F5ExternalLoadBalancerElement.java index f8824d951a5..348053ca7f5 100644 --- a/server/src/com/cloud/network/element/F5ExternalLoadBalancerElement.java +++ b/server/src/com/cloud/network/element/F5ExternalLoadBalancerElement.java @@ -458,7 +458,7 @@ public class F5ExternalLoadBalancerElement extends ExternalLoadBalancerDeviceMan } @Override - public boolean verifyServicesCombination(List services) { + public boolean verifyServicesCombination(Set services) { return true; } diff --git a/server/src/com/cloud/network/element/JuniperSRXExternalFirewallElement.java b/server/src/com/cloud/network/element/JuniperSRXExternalFirewallElement.java index 73577d6a3ea..a7c4a01e3ea 100644 --- a/server/src/com/cloud/network/element/JuniperSRXExternalFirewallElement.java +++ b/server/src/com/cloud/network/element/JuniperSRXExternalFirewallElement.java @@ -535,7 +535,7 @@ PortForwardingServiceProvider, RemoteAccessVPNServiceProvider, IpDeployer, Junip } @Override - public boolean verifyServicesCombination(List services) { + public boolean verifyServicesCombination(Set services) { return true; } diff --git a/server/src/com/cloud/network/element/NetscalerElement.java b/server/src/com/cloud/network/element/NetscalerElement.java index 63d9b654189..6f6f17c6c46 100644 --- a/server/src/com/cloud/network/element/NetscalerElement.java +++ b/server/src/com/cloud/network/element/NetscalerElement.java @@ -15,6 +15,7 @@ package com.cloud.network.element; import java.net.URI; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -543,15 +544,19 @@ StaticNatServiceProvider { } @Override - public boolean verifyServicesCombination(List services) { - List netscalerServices = new ArrayList(); - netscalerServices.add(Service.Lb.getName()); - netscalerServices.add(Service.StaticNat.getName()); + public boolean verifyServicesCombination(Set services) { + Set netscalerServices = new HashSet(); + netscalerServices.add(Service.Lb); + netscalerServices.add(Service.StaticNat); // NetScaler can only act as Lb and Static Nat service provider if (services != null && !services.isEmpty() && !netscalerServices.containsAll(services)) { + String servicesList = ""; + for (Service service : services) { + servicesList += service.getName() + " "; + } s_logger.warn("NetScaler network element can only support LB and Static NAT services and service combination " - + services + " is not supported."); + + servicesList + " is not supported."); return false; } diff --git a/server/src/com/cloud/network/element/OvsElement.java b/server/src/com/cloud/network/element/OvsElement.java index 9a736671593..b27c36e69cd 100644 --- a/server/src/com/cloud/network/element/OvsElement.java +++ b/server/src/com/cloud/network/element/OvsElement.java @@ -14,6 +14,7 @@ package com.cloud.network.element; import java.util.List; import java.util.Map; +import java.util.Set; import javax.ejb.Local; @@ -126,7 +127,7 @@ public class OvsElement extends AdapterBase implements NetworkElement { } @Override - public boolean verifyServicesCombination(List services) { + public boolean verifyServicesCombination(Set services) { return true; } } diff --git a/server/src/com/cloud/network/element/SecurityGroupElement.java b/server/src/com/cloud/network/element/SecurityGroupElement.java index db4dd52ce2e..ef4dc1fe945 100644 --- a/server/src/com/cloud/network/element/SecurityGroupElement.java +++ b/server/src/com/cloud/network/element/SecurityGroupElement.java @@ -15,6 +15,7 @@ package com.cloud.network.element; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; import javax.ejb.Local; @@ -101,7 +102,7 @@ public class SecurityGroupElement extends AdapterBase implements NetworkElement } @Override - public boolean verifyServicesCombination(List services) { + public boolean verifyServicesCombination(Set services) { return true; } } diff --git a/server/src/com/cloud/network/element/VirtualRouterElement.java b/server/src/com/cloud/network/element/VirtualRouterElement.java index c7ccc4b343f..c09318fe0a5 100755 --- a/server/src/com/cloud/network/element/VirtualRouterElement.java +++ b/server/src/com/cloud/network/element/VirtualRouterElement.java @@ -53,8 +53,8 @@ import com.cloud.network.dao.VirtualRouterProviderDao; import com.cloud.network.lb.LoadBalancingRule; import com.cloud.network.lb.LoadBalancingRule.LbStickinessPolicy; import com.cloud.network.lb.LoadBalancingRulesManager; -import com.cloud.network.router.VirtualNetworkApplianceManager; import com.cloud.network.router.VirtualRouter.Role; +import com.cloud.network.router.VpcVirtualNetworkApplianceManager; import com.cloud.network.rules.FirewallRule; import com.cloud.network.rules.LbStickinessMethod; import com.cloud.network.rules.LbStickinessMethod.StickinessMethodType; @@ -102,7 +102,7 @@ LoadBalancingServiceProvider, PortForwardingServiceProvider, RemoteAccessVPNServ @Inject NetworkOfferingDao _networkOfferingDao; @Inject - VirtualNetworkApplianceManager _routerMgr; + VpcVirtualNetworkApplianceManager _routerMgr; @Inject ConfigurationManager _configMgr; @Inject @@ -869,11 +869,16 @@ LoadBalancingServiceProvider, PortForwardingServiceProvider, RemoteAccessVPNServ } @Override - public boolean verifyServicesCombination(List services) { - if (!services.contains("SourceNat")) { - if (services.contains("StaticNat") || services.contains("Firewall") || services.contains("Lb") || services.contains("PortForwarding") || - services.contains("Vpn")) { - s_logger.warn("Virtual router can't enable services " + services + " without source NAT service"); + public boolean verifyServicesCombination(Set services) { + if (!services.contains(Service.SourceNat)) { + if (services.contains(Service.StaticNat) || services.contains(Service.Firewall) || services.contains(Service.Lb) || + services.contains(Service.PortForwarding) || services.contains(Service.Vpn)) { + String servicesList = "["; + for (Service service : services) { + servicesList += service.getName() + " "; + } + servicesList += "]"; + s_logger.warn("Virtual router can't enable services " + servicesList + " without source NAT service"); return false; } } diff --git a/server/src/com/cloud/network/lb/ElasticLoadBalancerManagerImpl.java b/server/src/com/cloud/network/lb/ElasticLoadBalancerManagerImpl.java index 8b786a2f519..ce38e6d4a52 100644 --- a/server/src/com/cloud/network/lb/ElasticLoadBalancerManagerImpl.java +++ b/server/src/com/cloud/network/lb/ElasticLoadBalancerManagerImpl.java @@ -89,7 +89,6 @@ import com.cloud.network.dao.VirtualRouterProviderDao; import com.cloud.network.lb.LoadBalancingRule.LbDestination; import com.cloud.network.lb.LoadBalancingRule.LbStickinessPolicy; import com.cloud.network.lb.dao.ElasticLbVmMapDao; -import com.cloud.network.router.VirtualNetworkApplianceManager; import com.cloud.network.router.VirtualRouter; import com.cloud.network.router.VirtualRouter.RedundantState; import com.cloud.network.router.VirtualRouter.Role; @@ -149,8 +148,6 @@ ElasticLoadBalancerManager, Manager, VirtualMachineGuru { @Inject LoadBalancingRulesManager _lbMgr; @Inject - VirtualNetworkApplianceManager _routerMgr; - @Inject DomainRouterDao _routerDao = null; @Inject protected HostPodDao _podDao = null; diff --git a/server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java b/server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java index 8ed22d3ccbd..00aa9f757ef 100755 --- a/server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java +++ b/server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java @@ -1,1549 +1,1558 @@ -// 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.network.lb; - -import java.security.InvalidParameterException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import javax.ejb.Local; -import javax.naming.ConfigurationException; - -import org.apache.log4j.Logger; - -import com.cloud.api.commands.CreateLBStickinessPolicyCmd; -import com.cloud.api.commands.CreateLoadBalancerRuleCmd; -import com.cloud.api.commands.ListLBStickinessPoliciesCmd; -import com.cloud.api.commands.ListLoadBalancerRuleInstancesCmd; -import com.cloud.api.commands.ListLoadBalancerRulesCmd; -import com.cloud.api.commands.UpdateLoadBalancerRuleCmd; -import com.cloud.api.response.ServiceResponse; -import com.cloud.configuration.Config; -import com.cloud.configuration.ConfigurationManager; -import com.cloud.configuration.dao.ConfigurationDao; -import com.cloud.dc.dao.DataCenterDao; -import com.cloud.dc.dao.VlanDao; -import com.cloud.domain.dao.DomainDao; -import com.cloud.event.ActionEvent; -import com.cloud.event.EventTypes; -import com.cloud.event.UsageEventVO; -import com.cloud.event.dao.EventDao; -import com.cloud.event.dao.UsageEventDao; -import com.cloud.exception.InsufficientAddressCapacityException; -import com.cloud.exception.InvalidParameterValueException; -import com.cloud.exception.NetworkRuleConflictException; -import com.cloud.exception.PermissionDeniedException; -import com.cloud.exception.ResourceUnavailableException; -import com.cloud.network.ExternalLoadBalancerUsageManager; -import com.cloud.network.IPAddressVO; -import com.cloud.network.IpAddress; -import com.cloud.network.LBStickinessPolicyVO; -import com.cloud.network.LoadBalancerVMMapVO; -import com.cloud.network.LoadBalancerVO; -import com.cloud.network.Network; -import com.cloud.network.Network.Capability; -import com.cloud.network.Network.Provider; -import com.cloud.network.Network.Service; -import com.cloud.network.NetworkManager; -import com.cloud.network.NetworkVO; -import com.cloud.network.as.AutoScalePolicy; -import com.cloud.network.as.AutoScalePolicyConditionMapVO; -import com.cloud.network.as.AutoScaleVmGroup; -import com.cloud.network.as.AutoScaleVmGroupPolicyMapVO; -import com.cloud.network.as.AutoScaleVmGroupVO; -import com.cloud.network.as.AutoScaleVmProfile; -import com.cloud.network.as.Condition; -import com.cloud.network.as.Counter; -import com.cloud.network.as.dao.AutoScalePolicyConditionMapDao; -import com.cloud.network.as.dao.AutoScalePolicyDao; -import com.cloud.network.as.dao.AutoScaleVmGroupDao; -import com.cloud.network.as.dao.AutoScaleVmGroupPolicyMapDao; -import com.cloud.network.as.dao.AutoScaleVmProfileDao; -import com.cloud.network.as.dao.ConditionDao; -import com.cloud.network.as.dao.CounterDao; -import com.cloud.network.dao.FirewallRulesCidrsDao; -import com.cloud.network.dao.FirewallRulesDao; -import com.cloud.network.dao.IPAddressDao; -import com.cloud.network.dao.LBStickinessPolicyDao; -import com.cloud.network.dao.LoadBalancerDao; -import com.cloud.network.dao.LoadBalancerVMMapDao; -import com.cloud.network.dao.NetworkDao; -import com.cloud.network.dao.NetworkServiceMapDao; -import com.cloud.network.lb.LoadBalancingRule.LbAutoScalePolicy; -import com.cloud.network.lb.LoadBalancingRule.LbAutoScaleVmGroup; -import com.cloud.network.lb.LoadBalancingRule.LbAutoScaleVmProfile; -import com.cloud.network.lb.LoadBalancingRule.LbCondition; -import com.cloud.network.lb.LoadBalancingRule.LbDestination; -import com.cloud.network.lb.LoadBalancingRule.LbStickinessPolicy; -import com.cloud.network.rules.FirewallManager; -import com.cloud.network.rules.FirewallRule; -import com.cloud.network.rules.FirewallRule.FirewallRuleType; -import com.cloud.network.rules.FirewallRule.Purpose; -import com.cloud.network.rules.FirewallRuleVO; -import com.cloud.network.rules.LbStickinessMethod; -import com.cloud.network.rules.LbStickinessMethod.LbStickinessMethodParam; -import com.cloud.network.rules.LoadBalancer; -import com.cloud.network.rules.RulesManager; -import com.cloud.network.rules.StickinessPolicy; -import com.cloud.network.vpc.VpcManager; -import com.cloud.offering.NetworkOffering; -import com.cloud.projects.Project.ListProjectResourcesCriteria; -import com.cloud.server.ResourceTag.TaggedResourceType; -import com.cloud.tags.ResourceTagVO; -import com.cloud.tags.dao.ResourceTagDao; -import com.cloud.template.TemplateManager; -import com.cloud.user.Account; -import com.cloud.user.AccountManager; -import com.cloud.user.DomainService; -import com.cloud.user.User; -import com.cloud.user.UserContext; -import com.cloud.user.dao.AccountDao; -import com.cloud.user.dao.UserDao; -import com.cloud.uservm.UserVm; -import com.cloud.utils.IdentityProxy; -import com.cloud.utils.Ternary; -import com.cloud.utils.component.Inject; -import com.cloud.utils.component.Manager; -import com.cloud.utils.db.DB; -import com.cloud.utils.db.Filter; -import com.cloud.utils.db.JoinBuilder; -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.net.NetUtils; -import com.cloud.vm.Nic; -import com.cloud.vm.UserVmVO; -import com.cloud.vm.VirtualMachine.State; -import com.cloud.vm.dao.NicDao; -import com.cloud.vm.dao.UserVmDao; -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; - -@Local(value = { LoadBalancingRulesManager.class, LoadBalancingRulesService.class }) -public class LoadBalancingRulesManagerImpl implements LoadBalancingRulesManager, LoadBalancingRulesService, Manager { - private static final Logger s_logger = Logger.getLogger(LoadBalancingRulesManagerImpl.class); - - String _name; - - @Inject - NetworkManager _networkMgr; - @Inject - RulesManager _rulesMgr; - @Inject - AccountManager _accountMgr; - @Inject - IPAddressDao _ipAddressDao; - @Inject - LoadBalancerDao _lbDao; - @Inject - VlanDao _vlanDao; - @Inject - EventDao _eventDao; - @Inject - LoadBalancerVMMapDao _lb2VmMapDao; - @Inject - LBStickinessPolicyDao _lb2stickinesspoliciesDao; - @Inject - UserVmDao _vmDao; - @Inject - AccountDao _accountDao; - @Inject - DomainDao _domainDao; - @Inject - NicDao _nicDao; - @Inject - UsageEventDao _usageEventDao; - @Inject - FirewallRulesCidrsDao _firewallCidrsDao; - @Inject - FirewallManager _firewallMgr; - @Inject - ElasticLoadBalancerManager _elbMgr; - @Inject - NetworkDao _networkDao; - @Inject - FirewallRulesDao _firewallDao; - @Inject - DomainService _domainMgr; - @Inject - ConfigurationManager _configMgr; - @Inject - TemplateManager _templateMgr; - @Inject - ExternalLoadBalancerUsageManager _externalLBUsageMgr; - @Inject - NetworkServiceMapDao _ntwkSrvcDao; - @Inject - ResourceTagDao _resourceTagDao; - @Inject - CounterDao _counterDao; - @Inject - ConditionDao _conditionDao; - @Inject - AutoScaleVmProfileDao _autoScaleVmProfileDao; - @Inject - AutoScalePolicyDao _autoScalePolicyDao; - @Inject - AutoScalePolicyConditionMapDao _autoScalePolicyConditionMapDao; - @Inject - AutoScaleVmGroupDao _autoScaleVmGroupDao; - @Inject - AutoScaleVmGroupPolicyMapDao _autoScaleVmGroupPolicyMapDao; - @Inject - ConfigurationDao _configDao; - @Inject - DataCenterDao _dcDao = null; - @Inject - UserDao _userDao; - @Inject - VpcManager _vpcMgr; - - // Will return a string. For LB Stickiness this will be a json, for autoscale this will be "," separated values - @Override - public String getLBCapability(long networkid, String capabilityName) { - Map> serviceCapabilitiesMap = _networkMgr.getNetworkCapabilities(networkid); - if (serviceCapabilitiesMap != null) { - for (Service service : serviceCapabilitiesMap.keySet()) { - ServiceResponse serviceResponse = new ServiceResponse(); - serviceResponse.setName(service.getName()); - if ("Lb".equalsIgnoreCase(service.getName())) { - Map serviceCapabilities = serviceCapabilitiesMap - .get(service); - if (serviceCapabilities != null) { - for (Capability capability : serviceCapabilities - .keySet()) { - if (capabilityName.equals(capability.getName())) { - return serviceCapabilities.get(capability); - } - } - } - } - } - } - return null; - } - - private LbAutoScaleVmGroup getLbAutoScaleVmGroup(AutoScaleVmGroup vmGroup) { - List vmGroupPolicyMapList = _autoScaleVmGroupPolicyMapDao.listByVmGroupId(vmGroup.getId()); - List autoScalePolicies = new ArrayList(); - for (AutoScaleVmGroupPolicyMapVO vmGroupPolicyMap : vmGroupPolicyMapList) { - AutoScalePolicy autoScalePolicy = _autoScalePolicyDao.findById(vmGroupPolicyMap.getPolicyId()); - List autoScalePolicyConditionMapList = _autoScalePolicyConditionMapDao.listByAll(autoScalePolicy.getId(), null); - List lbConditions = new ArrayList(); - for (AutoScalePolicyConditionMapVO autoScalePolicyConditionMap : autoScalePolicyConditionMapList) { - Condition condition = _conditionDao.findById(autoScalePolicyConditionMap.getConditionId()); - Counter counter = _counterDao.findById(condition.getCounterid()); - lbConditions.add(new LbCondition(counter, condition)); - } - autoScalePolicies.add(new LbAutoScalePolicy(autoScalePolicy, lbConditions)); - } - AutoScaleVmProfile autoScaleVmProfile = _autoScaleVmProfileDao.findById(vmGroup.getProfileId()); - Long autoscaleUserId = autoScaleVmProfile.getAutoScaleUserId(); - User user = _userDao.findById(autoscaleUserId); - String apiKey = user.getApiKey(); - String secretKey = user.getSecretKey(); - String csUrl = _configDao.getValue(Config.EndpointeUrl.key()); - - if(apiKey == null) { - 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", 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", null); - } - - LbAutoScaleVmProfile lbAutoScaleVmProfile = new LbAutoScaleVmProfile(autoScaleVmProfile, apiKey, secretKey, csUrl); - return new LbAutoScaleVmGroup(vmGroup, autoScalePolicies, lbAutoScaleVmProfile); - } - - private boolean applyAutoScaleConfig(LoadBalancerVO lb, LoadBalancingRule rule) throws ResourceUnavailableException { - if (!isRollBackAllowedForProvider(lb)) { - // this is for Netscalar type of devices. if their is failure the db entries will be rollbacked. - return false; - } - - List rules = Arrays.asList(rule); - - if (!_networkMgr.applyRules(rules, false)) { - s_logger.debug("LB rules' autoscale config are not completely applied"); - return false; - } - - return true; - } - - @Override - @DB - public boolean configureLbAutoScaleVmGroup(long vmGroupid) { - AutoScaleVmGroupVO vmGroup = _autoScaleVmGroupDao.findById(vmGroupid); - boolean success = false; - - LoadBalancerVO loadBalancer = _lbDao.findById(vmGroup.getLoadBalancerId()); - - FirewallRule.State backupState = loadBalancer.getState(); - - if (vmGroup.getState().equals(AutoScaleVmGroup.State_New)) { - loadBalancer.setState(FirewallRule.State.Add); - _lbDao.persist(loadBalancer); - } - else if (loadBalancer.getState() == FirewallRule.State.Active && - vmGroup.getState().equals(AutoScaleVmGroup.State_Revoke)) { - loadBalancer.setState(FirewallRule.State.Add); - _lbDao.persist(loadBalancer); - } - - // LBTODO - try { - LbAutoScaleVmGroup lbAutoScaleVmGroup = getLbAutoScaleVmGroup(vmGroup); - LoadBalancingRule rule = new LoadBalancingRule(loadBalancer, null, null); - rule.setAutoScaleVmGroup(lbAutoScaleVmGroup); - success = applyAutoScaleConfig(loadBalancer, rule); - } catch (ResourceUnavailableException e) { - s_logger.warn("Unable to configure AutoScaleVmGroup to the lb rule: " + loadBalancer.getId() + " because resource is unavaliable:", e); - } finally { - if (!success) { - s_logger.warn("Failed to configure LB Auto Scale Vm Group with Id:" + vmGroupid); - if (isRollBackAllowedForProvider(loadBalancer)) { - loadBalancer.setState(backupState); - _lbDao.persist(loadBalancer); - s_logger.debug("LB Rollback rule id: " + loadBalancer.getId() + " lb state rolback while creating AutoscaleVmGroup"); - } - } - } - - if (success) { - if (vmGroup.getState().equals(AutoScaleVmGroup.State_New)) { - Transaction.currentTxn().start(); - loadBalancer.setState(FirewallRule.State.Active); - s_logger.debug("LB rule " + loadBalancer.getId() + " state is set to Active"); - _lbDao.persist(loadBalancer); - vmGroup.setState(AutoScaleVmGroup.State_Enabled); - _autoScaleVmGroupDao.persist(vmGroup); - s_logger.debug("LB Auto Scale Vm Group with Id: " + vmGroupid + " is set to Enabled state."); - Transaction.currentTxn().commit(); - } - s_logger.info("Successfully configured LB Autoscale Vm Group with Id: " + vmGroupid); - } - return success; - } - - private boolean genericValidator(CreateLBStickinessPolicyCmd cmd) throws InvalidParameterValueException { - LoadBalancerVO loadBalancer = _lbDao.findById(cmd.getLbRuleId()); - /* Validation : check for valid Method name and params */ - List stickinessMethodList = getStickinessMethods(loadBalancer - .getNetworkId()); - boolean methodMatch = false; - - if (stickinessMethodList == null) { - List idList = new ArrayList(); - idList.add(new IdentityProxy("firewall_rules", cmd.getLbRuleId(), "ruleId")); - throw new InvalidParameterValueException("Failed: No Stickiness method available for LB rule with specified id", idList); - } - for (LbStickinessMethod method : stickinessMethodList) { - if (method.getMethodName().equalsIgnoreCase(cmd.getStickinessMethodName())) { - methodMatch = true; - Map apiParamList = cmd.getparamList(); - List methodParamList = method.getParamList(); - Map tempParamList = new HashMap(); - - /* - * validation-1: check for any extra params that are not - * required by the policymethod(capability), FIXME: make the - * below loop simple without using raw data type - */ - if (apiParamList != null) { - Collection userGroupCollection = apiParamList.values(); - Iterator iter = userGroupCollection.iterator(); - while (iter.hasNext()) { - HashMap paramKVpair = (HashMap) iter.next(); - String paramName = paramKVpair.get("name"); - String paramValue = paramKVpair.get("value"); - - tempParamList.put(paramName, paramValue); - Boolean found = false; - for (LbStickinessMethodParam param : methodParamList) { - if (param.getParamName().equalsIgnoreCase(paramName)) { - if ((param.getIsflag() == false) && (paramValue == null)) { - throw new InvalidParameterValueException("Failed : Value expected for the Param :" + param.getParamName(), null); - } - found = true; - break; - } - } - if (!found) { - throw new InvalidParameterValueException("Failed : Stickiness policy does not support param name :" + paramName, null); - } - } - } - - /* validation-2: check for mandatory params */ - for (LbStickinessMethodParam param : methodParamList) { - if (param.getRequired()) { - if (tempParamList.get(param.getParamName()) == null) { - throw new InvalidParameterValueException("Failed : Missing Manadatory Param :" + param.getParamName(), null); - } - } - } - /* Successfully completed the Validation */ - break; - } - } - if (methodMatch == false) { - List idList = new ArrayList(); - idList.add(new IdentityProxy("firewall_rules", cmd.getLbRuleId(), "ruleId")); - throw new InvalidParameterValueException("Failed to match Stickiness method name for LB rule whose id is specified", idList); - } - - /* Validation : check for the multiple policies to the rule id */ - List stickinessPolicies = _lb2stickinesspoliciesDao.listByLoadBalancerId(cmd.getLbRuleId(), false); - if (stickinessPolicies.size() > 0) { - List idList = new ArrayList(); - idList.add(new IdentityProxy("firewall_rules", cmd.getLbRuleId(), "ruleId")); - throw new InvalidParameterValueException("Failed to create Stickiness policy: Policy already attached", idList); - } - return true; - } - - @SuppressWarnings("rawtypes") - @Override - @DB - @ActionEvent(eventType = EventTypes.EVENT_LB_STICKINESSPOLICY_CREATE, eventDescription = "create lb stickinesspolicy to load balancer", create = true) - public StickinessPolicy createLBStickinessPolicy(CreateLBStickinessPolicyCmd cmd) throws NetworkRuleConflictException { - UserContext caller = UserContext.current(); - - /* Validation : check corresponding load balancer rule exist */ - LoadBalancerVO loadBalancer = _lbDao.findById(cmd.getLbRuleId()); - if (loadBalancer == null) { - throw new InvalidParameterValueException("Failed: LB rule provided not present", null); - } - - _accountMgr.checkAccess(caller.getCaller(), null, true, loadBalancer); - if (loadBalancer.getState() == FirewallRule.State.Revoke) { - List idList = new ArrayList(); - idList.add(new IdentityProxy("firewall_rules", cmd.getLbRuleId(), "ruleId")); - throw new InvalidParameterValueException("Failed: LB rule with specified id is in deleting state: ", idList); - } - - /* Generic validations */ - if (!genericValidator(cmd)) { - List idList = new ArrayList(); - idList.add(new IdentityProxy("firewall_rules", cmd.getLbRuleId(), "ruleId")); - throw new InvalidParameterValueException("Failed to create Stickiness policy: Validation of rule with specified id failed", idList); - } - - /* Specific validations using network element validator for specific validations */ - LBStickinessPolicyVO lbpolicy = new LBStickinessPolicyVO(loadBalancer.getId(), cmd.getLBStickinessPolicyName(), cmd.getStickinessMethodName(), cmd.getparamList(), cmd.getDescription()); - List policyList = new ArrayList(); - policyList.add(new LbStickinessPolicy(cmd.getStickinessMethodName(), lbpolicy.getParams())); - LoadBalancingRule lbRule = new LoadBalancingRule(loadBalancer, getExistingDestinations(lbpolicy.getId()), policyList); - if (!_networkMgr.validateRule(lbRule)) { - List idList = new ArrayList(); - idList.add(new IdentityProxy("firewall_rules", cmd.getLbRuleId(), "ruleId")); - throw new InvalidParameterValueException("Failed to create Stickiness policy: Validation of rule with specified id failed ", idList); - } - - /* Finally Insert into DB */ - LBStickinessPolicyVO policy = new LBStickinessPolicyVO(loadBalancer.getId(), cmd.getLBStickinessPolicyName(), cmd.getStickinessMethodName(), cmd.getparamList(), cmd.getDescription()); - policy = _lb2stickinesspoliciesDao.persist(policy); - - return policy; - } - - @Override - @DB - @ActionEvent(eventType = EventTypes.EVENT_LB_STICKINESSPOLICY_CREATE, eventDescription = "Apply Stickinesspolicy to load balancer ", async = true) - public boolean applyLBStickinessPolicy(CreateLBStickinessPolicyCmd cmd) { - boolean success = true; - - LoadBalancerVO loadBalancer = _lbDao.findById(cmd.getLbRuleId()); - if (loadBalancer == null) { - throw new InvalidParameterException("Invalid Load balancer Id:" + cmd.getLbRuleId()); - } - FirewallRule.State backupState = loadBalancer.getState(); - loadBalancer.setState(FirewallRule.State.Add); - _lbDao.persist(loadBalancer); - try { - applyLoadBalancerConfig(cmd.getLbRuleId()); - } catch (ResourceUnavailableException e) { - s_logger.warn("Unable to apply Stickiness policy to the lb rule: " + cmd.getLbRuleId() + " because resource is unavaliable:", e); - if (isRollBackAllowedForProvider(loadBalancer)) { - loadBalancer.setState(backupState); - _lbDao.persist(loadBalancer); - s_logger.debug("LB Rollback rule id: " + loadBalancer.getId() + " lb state rolback while creating sticky policy"); - } - deleteLBStickinessPolicy(cmd.getEntityId(), false); - success = false; - } - - return success; - } - - @Override - @ActionEvent(eventType = EventTypes.EVENT_LB_STICKINESSPOLICY_DELETE, eventDescription = "revoking LB Stickiness policy ", async = true) - public boolean deleteLBStickinessPolicy(long stickinessPolicyId, boolean apply) { - boolean success = true; - - UserContext caller = UserContext.current(); - LBStickinessPolicyVO stickinessPolicy = _lb2stickinesspoliciesDao.findById(stickinessPolicyId); - - if (stickinessPolicy == null) { - throw new InvalidParameterException("Invalid Stickiness policy id value: " + stickinessPolicyId); - } - LoadBalancerVO loadBalancer = _lbDao.findById(Long.valueOf(stickinessPolicy.getLoadBalancerId())); - if (loadBalancer == null) { - throw new InvalidParameterException("Invalid Load balancer : " + stickinessPolicy.getLoadBalancerId() + " for Stickiness policy id: " + stickinessPolicyId); - } - long loadBalancerId = loadBalancer.getId(); - FirewallRule.State backupState = loadBalancer.getState(); - _accountMgr.checkAccess(caller.getCaller(), null, true, loadBalancer); - - if (apply) { - if (loadBalancer.getState() == FirewallRule.State.Active) { - loadBalancer.setState(FirewallRule.State.Add); - _lbDao.persist(loadBalancer); - } - - boolean backupStickyState = stickinessPolicy.isRevoke(); - stickinessPolicy.setRevoke(true); - _lb2stickinesspoliciesDao.persist(stickinessPolicy); - s_logger.debug("Set load balancer rule for revoke: rule id " + loadBalancerId + ", stickinesspolicyID " + stickinessPolicyId); - - try { - if (!applyLoadBalancerConfig(loadBalancerId)) { - s_logger.warn("Failed to remove load balancer rule id " + loadBalancerId + " for stickinesspolicyID " + stickinessPolicyId); - throw new CloudRuntimeException("Failed to remove load balancer rule id " + loadBalancerId + " for stickinesspolicyID " + stickinessPolicyId); - } - } catch (ResourceUnavailableException e) { - if (isRollBackAllowedForProvider(loadBalancer)) { - stickinessPolicy.setRevoke(backupStickyState); - _lb2stickinesspoliciesDao.persist(stickinessPolicy); - loadBalancer.setState(backupState); - _lbDao.persist(loadBalancer); - s_logger.debug("LB Rollback rule id: " + loadBalancer.getId() + " while deleting sticky policy: " + stickinessPolicyId); - } - s_logger.warn("Unable to apply the load balancer config because resource is unavaliable.", e); - success = false; - } - } else { - _lb2stickinesspoliciesDao.remove(stickinessPolicy.getLoadBalancerId()); - } - - return success; - } - - private boolean isRollBackAllowedForProvider(LoadBalancerVO loadBalancer) { - Network network = _networkDao.findById(loadBalancer.getNetworkId()); - Provider provider = Network.Provider.Netscaler; - return _ntwkSrvcDao.canProviderSupportServiceInNetwork(network.getId(), Service.Lb, provider); - } - - @Override - @DB - @ActionEvent(eventType = EventTypes.EVENT_ASSIGN_TO_LOAD_BALANCER_RULE, eventDescription = "assigning to load balancer", async = true) - public boolean assignToLoadBalancer(long loadBalancerId, List instanceIds) { - UserContext ctx = UserContext.current(); - Account caller = ctx.getCaller(); - - LoadBalancerVO loadBalancer = _lbDao.findById(loadBalancerId); - if (loadBalancer == null) { - throw new InvalidParameterValueException("Failed to assign to load balancer; the load balancer was not found.", null); - } - - List mappedInstances = _lb2VmMapDao.listByLoadBalancerId(loadBalancerId, false); - Set mappedInstanceIds = new HashSet(); - for (LoadBalancerVMMapVO mappedInstance : mappedInstances) { - mappedInstanceIds.add(Long.valueOf(mappedInstance.getInstanceId())); - } - - List vmsToAdd = new ArrayList(); - - for (Long instanceId : instanceIds) { - if (mappedInstanceIds.contains(instanceId)) { - List idList = new ArrayList(); - idList.add(new IdentityProxy("user_vm", instanceId, "vmId")); - throw new InvalidParameterValueException("VM with specified id is already mapped to load balancer", idList); - } - - UserVm vm = _vmDao.findById(instanceId); - if (vm == null || vm.getState() == State.Destroyed || vm.getState() == State.Expunging) { - throw new InvalidParameterValueException("Couldn't locate vm instance by id", null); - } - - _rulesMgr.checkRuleAndUserVm(loadBalancer, vm, caller); - - if (vm.getAccountId() != loadBalancer.getAccountId()) { - throw new PermissionDeniedException("Cannot add virtual machines that do not belong to the same owner."); - } - - // Let's check to make sure the vm has a nic in the same network as the load balancing rule. - List nics = _networkMgr.getNics(vm.getId()); - Nic nicInSameNetwork = null; - for (Nic nic : nics) { - if (nic.getNetworkId() == loadBalancer.getNetworkId()) { - nicInSameNetwork = nic; - break; - } - } - - if (nicInSameNetwork == null) { - List idList = new ArrayList(); - idList.add(new IdentityProxy(vm, instanceId, "vmId")); - throw new InvalidParameterValueException("VM with specified id cannot be added because it doesn't belong in the same network.", idList); - - } - - if (s_logger.isDebugEnabled()) { - s_logger.debug("Adding " + vm + " to the load balancer pool"); - } - vmsToAdd.add(vm); - } - - Transaction txn = Transaction.currentTxn(); - txn.start(); - for (UserVm vm : vmsToAdd) { - LoadBalancerVMMapVO map = new LoadBalancerVMMapVO(loadBalancer.getId(), vm.getId(), false); - map = _lb2VmMapDao.persist(map); - } - txn.commit(); - - if (_autoScaleVmGroupDao.isAutoScaleLoadBalancer(loadBalancerId)) { - // Nothing needs to be done for an autoscaled loadbalancer, - // just persist and proceed. - return true; - } - - boolean success = false; - FirewallRule.State backupState = loadBalancer.getState(); - try { - loadBalancer.setState(FirewallRule.State.Add); - _lbDao.persist(loadBalancer); - applyLoadBalancerConfig(loadBalancerId); - success = true; - } catch (ResourceUnavailableException e) { - if (isRollBackAllowedForProvider(loadBalancer)) { - List vmInstanceIds = new ArrayList(); - txn = Transaction.currentTxn(); - txn.start(); - for (UserVm vm : vmsToAdd) { - vmInstanceIds.add(vm.getId()); - } - txn.commit(); - if (!vmInstanceIds.isEmpty()) { - _lb2VmMapDao.remove(loadBalancer.getId(), vmInstanceIds, null); - s_logger.debug("LB Rollback rule id: " + loadBalancer.getId() + " while attaching VM: " + vmInstanceIds); - } - loadBalancer.setState(backupState); - _lbDao.persist(loadBalancer); - } - s_logger.warn("Unable to apply the load balancer config because resource is unavaliable.", e); - } - - if (!success) { - CloudRuntimeException ex = new CloudRuntimeException("Failed to add specified loadbalancerruleid for vms " + instanceIds); - ex.addProxyObject(loadBalancer, loadBalancerId, "loadBalancerId"); - // TBD: Also pack in the instanceIds in the exception using the right VO object or table name. - throw ex; - } - - return success; - } - - @Override - @ActionEvent(eventType = EventTypes.EVENT_REMOVE_FROM_LOAD_BALANCER_RULE, eventDescription = "removing from load balancer", async = true) - public boolean removeFromLoadBalancer(long loadBalancerId, List instanceIds) { - return removeFromLoadBalancerInternal(loadBalancerId, instanceIds, true); - } - - private boolean removeFromLoadBalancerInternal(long loadBalancerId, List instanceIds, boolean rollBack) { - UserContext caller = UserContext.current(); - - LoadBalancerVO loadBalancer = _lbDao.findById(Long.valueOf(loadBalancerId)); - if (loadBalancer == null) { - throw new InvalidParameterException("Invalid load balancer value: " + loadBalancerId); - } - - _accountMgr.checkAccess(caller.getCaller(), null, true, loadBalancer); - - boolean success = false; - FirewallRule.State backupState = loadBalancer.getState(); - try { - loadBalancer.setState(FirewallRule.State.Add); - _lbDao.persist(loadBalancer); - - for (long instanceId : instanceIds) { - LoadBalancerVMMapVO map = _lb2VmMapDao.findByLoadBalancerIdAndVmId(loadBalancerId, instanceId); - map.setRevoke(true); - _lb2VmMapDao.persist(map); - s_logger.debug("Set load balancer rule for revoke: rule id " + loadBalancerId + ", vmId " + instanceId); - } - - if (_autoScaleVmGroupDao.isAutoScaleLoadBalancer(loadBalancerId)) { - // Nothing needs to be done for an autoscaled loadbalancer, - // just persist and proceed. - return true; - } - - if (!applyLoadBalancerConfig(loadBalancerId)) { - s_logger.warn("Failed to remove load balancer rule id " + loadBalancerId + " for vms " + instanceIds); - CloudRuntimeException ex = new CloudRuntimeException("Failed to remove specified load balancer rule id for vms " + instanceIds); - ex.addProxyObject(loadBalancer, loadBalancerId, "loadBalancerId"); - throw ex; - } - success = true; - } catch (ResourceUnavailableException e) { - if (rollBack && isRollBackAllowedForProvider(loadBalancer)) { - - for (long instanceId : instanceIds) { - LoadBalancerVMMapVO map = _lb2VmMapDao.findByLoadBalancerIdAndVmId(loadBalancerId, instanceId); - map.setRevoke(false); - _lb2VmMapDao.persist(map); - s_logger.debug("LB Rollback rule id: " + loadBalancerId + ",while removing vmId " + instanceId); - } - - loadBalancer.setState(backupState); - _lbDao.persist(loadBalancer); - s_logger.debug("LB Rollback rule id: " + loadBalancerId + " while removing vm instances"); - } - s_logger.warn("Unable to apply the load balancer config because resource is unavaliable.", e); - } - if (!success) { - CloudRuntimeException ex = new CloudRuntimeException("Failed to remove specified load balancer rule id for vms " + instanceIds); - ex.addProxyObject(loadBalancer, loadBalancerId, "loadBalancerId"); - throw ex; - } - return success; - } - - @Override - public boolean removeVmFromLoadBalancers(long instanceId) { - boolean success = true; - List maps = _lb2VmMapDao.listByInstanceId(instanceId); - if (maps == null || maps.isEmpty()) { - return true; - } - - Map> lbsToReconfigure = new HashMap>(); - - // first set all existing lb mappings with Revoke state - for (LoadBalancerVMMapVO map : maps) { - long lbId = map.getLoadBalancerId(); - List instances = lbsToReconfigure.get(lbId); - if (instances == null) { - instances = new ArrayList(); - } - instances.add(map.getInstanceId()); - lbsToReconfigure.put(lbId, instances); - - map.setRevoke(true); - _lb2VmMapDao.persist(map); - s_logger.debug("Set load balancer rule for revoke: rule id " + map.getLoadBalancerId() + ", vmId " + instanceId); - } - - // Reapply all lbs that had the vm assigned - if (lbsToReconfigure != null) { - for (Map.Entry> lb : lbsToReconfigure.entrySet()) { - if (!removeFromLoadBalancerInternal(lb.getKey(), lb.getValue(), false)) { - success = false; - } - } - } - return success; - } - - @Override - @ActionEvent(eventType = EventTypes.EVENT_LOAD_BALANCER_DELETE, eventDescription = "deleting load balancer", async = true) - public boolean deleteLoadBalancerRule(long loadBalancerId, boolean apply) { - UserContext ctx = UserContext.current(); - Account caller = ctx.getCaller(); - - LoadBalancerVO rule = _lbDao.findById(loadBalancerId); - if (rule == null) { - throw new InvalidParameterValueException("Unable to find load balancer rule by id", null); - } - - _accountMgr.checkAccess(caller, null, true, rule); - - boolean result = deleteLoadBalancerRule(loadBalancerId, apply, caller, ctx.getCallerUserId(), true); - if (!result) { - throw new CloudRuntimeException("Unable to remove load balancer rule " + loadBalancerId); - } - return result; - } - - @DB - public boolean deleteLoadBalancerRule(long loadBalancerId, boolean apply, Account caller, long callerUserId, boolean rollBack) { - LoadBalancerVO lb = _lbDao.findById(loadBalancerId); - Transaction txn = Transaction.currentTxn(); - boolean generateUsageEvent = false; - boolean success = true; - FirewallRule.State backupState = lb.getState(); - - txn.start(); - if (lb.getState() == FirewallRule.State.Staged) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Found a rule that is still in stage state so just removing it: " + lb); - } - generateUsageEvent = true; - } else if (lb.getState() == FirewallRule.State.Add || lb.getState() == FirewallRule.State.Active) { - lb.setState(FirewallRule.State.Revoke); - _lbDao.persist(lb); - generateUsageEvent = true; - } - List backupMaps = _lb2VmMapDao.listByLoadBalancerId(loadBalancerId); - List maps = _lb2VmMapDao.listByLoadBalancerId(loadBalancerId); - if (maps != null) { - for (LoadBalancerVMMapVO map : maps) { - map.setRevoke(true); - _lb2VmMapDao.persist(map); - s_logger.debug("Set load balancer rule for revoke: rule id " + loadBalancerId + ", vmId " + map.getInstanceId()); - } - } - - if (generateUsageEvent) { - // Generate usage event right after all rules were marked for revoke - UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_LOAD_BALANCER_DELETE, lb.getAccountId(), 0, lb.getId(), null); - _usageEventDao.persist(usageEvent); - } - - txn.commit(); - - // gather external network usage stats for this lb rule - NetworkVO network = _networkDao.findById(lb.getNetworkId()); - if (network != null) { - if (_networkMgr.networkIsConfiguredForExternalNetworking(network.getDataCenterId(), network.getId())) { - _externalLBUsageMgr.updateExternalLoadBalancerNetworkUsageStats(loadBalancerId); - } - } - - if (apply) { - try { - if (!applyLoadBalancerConfig(loadBalancerId)) { - s_logger.warn("Unable to apply the load balancer config"); - return false; - } - } catch (ResourceUnavailableException e) { - if (rollBack && isRollBackAllowedForProvider(lb)) { - if (backupMaps != null) { - for (LoadBalancerVMMapVO map : backupMaps) { - _lb2VmMapDao.persist(map); - s_logger.debug("LB Rollback rule id: " + loadBalancerId + ", vmId " + map.getInstanceId()); - } - } - lb.setState(backupState); - _lbDao.persist(lb); - s_logger.debug("LB Rollback rule id: " + loadBalancerId + " while deleting LB rule."); - } else { - s_logger.warn("Unable to apply the load balancer config because resource is unavaliable.", e); - } - return false; - } - } - - FirewallRuleVO relatedRule = _firewallDao.findByRelatedId(lb.getId()); - if (relatedRule != null) { - s_logger.warn("Unable to remove firewall rule id=" + lb.getId() + " as it has related firewall rule id=" + relatedRule.getId() + "; leaving it in Revoke state"); - success = false; - } else { - _firewallMgr.removeRule(lb); - } - - _elbMgr.handleDeleteLoadBalancerRule(lb, callerUserId, caller); - - if (success) { - s_logger.debug("Load balancer with id " + lb.getId() + " is removed successfully"); - } - - return success; - } - - @Override - @ActionEvent(eventType = EventTypes.EVENT_LOAD_BALANCER_CREATE, eventDescription = "creating load balancer") - public LoadBalancer createLoadBalancerRule(CreateLoadBalancerRuleCmd lb, boolean openFirewall) throws NetworkRuleConflictException, InsufficientAddressCapacityException { - Account lbOwner = _accountMgr.getAccount(lb.getEntityOwnerId()); - - int defPortStart = lb.getDefaultPortStart(); - int defPortEnd = lb.getDefaultPortEnd(); - - if (!NetUtils.isValidPort(defPortEnd)) { - throw new InvalidParameterValueException("privatePort is an invalid value: " + defPortEnd, null); - } - if (defPortStart > defPortEnd) { - throw new InvalidParameterValueException("private port range is invalid: " + defPortStart + "-" + defPortEnd, null); - } - if ((lb.getAlgorithm() == null) || !NetUtils.isValidAlgorithm(lb.getAlgorithm())) { - throw new InvalidParameterValueException("Invalid algorithm: " + lb.getAlgorithm(), null); - } - - Long ipAddrId = lb.getSourceIpAddressId(); - IPAddressVO ipVO = null; - if (ipAddrId != null) { - ipVO = _ipAddressDao.findById(ipAddrId); - } - - Network network = _networkMgr.getNetwork(lb.getNetworkId()); - - LoadBalancer result = _elbMgr.handleCreateLoadBalancerRule(lb, lbOwner, lb.getNetworkId()); - boolean performedIpAssoc = false; - if (result == null) { - IpAddress systemIp = null; - NetworkOffering off = _configMgr.getNetworkOffering(network.getNetworkOfferingId()); - if (off.getElasticLb() && ipVO == null && network.getVpcId() == null) { - systemIp = _networkMgr.assignSystemIp(lb.getNetworkId(), lbOwner, true, false); - lb.setSourceIpAddressId(systemIp.getId()); - ipVO = _ipAddressDao.findById(systemIp.getId()); - } - - // Validate ip address - if (ipVO == null) { - throw new InvalidParameterValueException("Unable to create load balance rule; can't find/allocate source IP", null); - } else if (ipVO.isOneToOneNat()) { - throw new NetworkRuleConflictException("Can't do load balance on ip address: " + ipVO.getAddress()); - } - - try { - if (ipVO.getAssociatedWithNetworkId() == null) { - boolean assignToVpcNtwk = network.getVpcId() != null - && ipVO.getVpcId() != null && ipVO.getVpcId().longValue() == network.getVpcId(); - if (assignToVpcNtwk) { - // set networkId just for verification purposes - _networkMgr.checkIpForService(ipVO, Service.Lb, lb.getNetworkId()); - - s_logger.debug("The ip is not associated with the VPC network id=" + lb.getNetworkId() + " so assigning"); - ipVO = _networkMgr.associateIPToGuestNetwork(ipAddrId, lb.getNetworkId(), false); - performedIpAssoc = true; - } - } else { - _networkMgr.checkIpForService(ipVO, Service.Lb, null); - } - - if (ipVO.getAssociatedWithNetworkId() == null) { - throw new InvalidParameterValueException("Ip address " + ipVO + " is not assigned to the network " + network, null); - } - - if (lb.getSourceIpAddressId() == null) { - throw new CloudRuntimeException("No ip address is defined to assign the LB to"); - } - result = createLoadBalancer(lb, openFirewall); - } catch (Exception ex) { - s_logger.warn("Failed to create load balancer due to ", ex); - if (ex instanceof NetworkRuleConflictException) { - throw (NetworkRuleConflictException) ex; - } - } finally { - if (result == null && systemIp != null) { - s_logger.debug("Releasing system IP address " + systemIp + " as corresponding lb rule failed to create"); - _networkMgr.handleSystemIpRelease(systemIp); - } - // release ip address if ipassoc was perfored - if (performedIpAssoc) { - ipVO = _ipAddressDao.findById(ipVO.getId()); - _vpcMgr.unassignIPFromVpcNetwork(ipVO.getId(), lb.getNetworkId()); - - } - } - } - - if (result == null) { - throw new CloudRuntimeException("Failed to create load balancer rule: " + lb.getName()); - } - - return result; - } - - @Override - @DB - public LoadBalancer createLoadBalancer(CreateLoadBalancerRuleCmd lb, boolean openFirewall) throws NetworkRuleConflictException { - UserContext caller = UserContext.current(); - int srcPortStart = lb.getSourcePortStart(); - int defPortStart = lb.getDefaultPortStart(); - int srcPortEnd = lb.getSourcePortEnd(); - long sourceIpId = lb.getSourceIpAddressId(); - - IPAddressVO ipAddr = _ipAddressDao.findById(sourceIpId); - // make sure ip address exists - if (ipAddr == null || !ipAddr.readyToUse()) { - InvalidParameterValueException ex = new InvalidParameterValueException("Unable to create load balancer rule, invalid IP address id specified", null); - ex.addProxyObject(ipAddr, sourceIpId, "sourceIpId"); - throw ex; - } else if (ipAddr.isOneToOneNat()) { - InvalidParameterValueException ex = new InvalidParameterValueException("Unable to create load balancer rule; specified sourceip id has static nat enabled", null); - ex.addProxyObject(ipAddr, sourceIpId, "sourceIpId"); - throw ex; - } - - _firewallMgr.validateFirewallRule(caller.getCaller(), ipAddr, srcPortStart, srcPortEnd, lb.getProtocol(), - Purpose.LoadBalancing, FirewallRuleType.User); - - Long networkId = ipAddr.getAssociatedWithNetworkId(); - if (networkId == null) { - List idList = new ArrayList(); - idList.add(new IdentityProxy(ipAddr, sourceIpId, "sourceIpid")); - throw new InvalidParameterValueException("Unable to create load balancer rule ; specified sourceip id is not associated with any network", idList); - - } - NetworkVO network = _networkDao.findById(networkId); - - _accountMgr.checkAccess(caller.getCaller(), null, true, ipAddr); - - // verify that lb service is supported by the network - if (!_networkMgr.areServicesSupportedInNetwork(network.getId(), Service.Lb)) { - List idList = new ArrayList(); - idList.add(new IdentityProxy(network, networkId, "networkId")); - throw new InvalidParameterValueException("LB service is not supported in network with specified id", idList); - } - - Transaction txn = Transaction.currentTxn(); - txn.start(); - - LoadBalancerVO newRule = new LoadBalancerVO(lb.getXid(), lb.getName(), lb.getDescription(), lb.getSourceIpAddressId(), lb.getSourcePortEnd(), lb.getDefaultPortStart(), - lb.getAlgorithm(), network.getId(), ipAddr.getAllocatedToAccountId(), ipAddr.getAllocatedInDomainId()); - - newRule = _lbDao.persist(newRule); - - if (openFirewall) { - _firewallMgr.createRuleForAllCidrs(sourceIpId, caller.getCaller(), lb.getSourcePortStart(), - lb.getSourcePortEnd(), lb.getProtocol(), null, null, newRule.getId(), networkId); - } - - boolean success = true; - - try { - _firewallMgr.detectRulesConflict(newRule); - if (!_firewallDao.setStateToAdd(newRule)) { - throw new CloudRuntimeException("Unable to update the state to add for " + newRule); - } - s_logger.debug("Load balancer " + newRule.getId() + " for Ip address id=" + sourceIpId + ", public port " + srcPortStart + ", private port " + defPortStart + " is added successfully."); - UserContext.current().setEventDetails("Load balancer Id: " + newRule.getId()); - UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_LOAD_BALANCER_CREATE, ipAddr.getAllocatedToAccountId(), ipAddr.getDataCenterId(), newRule.getId(), null); - _usageEventDao.persist(usageEvent); - txn.commit(); - - return newRule; - } catch (Exception e) { - success = false; - if (e instanceof NetworkRuleConflictException) { - throw (NetworkRuleConflictException) e; - } - throw new CloudRuntimeException("Unable to add rule for ip address id=" + newRule.getSourceIpAddressId(), e); - } finally { - if (!success && newRule != null) { - - txn.start(); - _firewallMgr.revokeRelatedFirewallRule(newRule.getId(), false); - removeLBRule(newRule); - - txn.commit(); - } - } - } - - @Override - public boolean applyLoadBalancerConfig(long lbRuleId) throws ResourceUnavailableException { - LoadBalancerVO lb = _lbDao.findById(lbRuleId); - List lbs; - if (isRollBackAllowedForProvider(lb)) { - // this is for Netscalar type of devices. if their is failure the db entries will be rollbacked. - lbs = Arrays.asList(lb); - } else { - // get all rules in transition state - lbs = _lbDao.listInTransitionStateByNetworkId(lb.getNetworkId()); - } - return applyLoadBalancerRules(lbs, true); - } - - @Override - public boolean applyLoadBalancersForNetwork(long networkId) throws ResourceUnavailableException { - List lbs = _lbDao.listByNetworkId(networkId); - if (lbs != null) { - return applyLoadBalancerRules(lbs, true); - } else { - s_logger.info("Network id=" + networkId + " doesn't have load balancer rules, nothing to apply"); - return true; - } - } - - @DB - protected boolean applyLoadBalancerRules(List lbs, boolean updateRulesInDB) throws ResourceUnavailableException { - Transaction txn = Transaction.currentTxn(); - List rules = new ArrayList(); - for (LoadBalancerVO lb : lbs) { - List dstList = getExistingDestinations(lb.getId()); - List policyList = getStickinessPolicies(lb.getId()); - - LoadBalancingRule loadBalancing = new LoadBalancingRule(lb, dstList, policyList); - rules.add(loadBalancing); - } - - if (!_networkMgr.applyRules(rules, false)) { - s_logger.debug("LB rules are not completely applied"); - return false; - } - - if (updateRulesInDB) { - for (LoadBalancerVO lb : lbs) { - boolean checkForReleaseElasticIp = false; - txn.start(); - if (lb.getState() == FirewallRule.State.Revoke) { - removeLBRule(lb); - s_logger.debug("LB " + lb.getId() + " is successfully removed"); - checkForReleaseElasticIp = true; - } else if (lb.getState() == FirewallRule.State.Add) { - lb.setState(FirewallRule.State.Active); - s_logger.debug("LB rule " + lb.getId() + " state is set to Active"); - _lbDao.persist(lb); - } - - // remove LB-Vm mappings that were state to revoke - List lbVmMaps = _lb2VmMapDao.listByLoadBalancerId(lb.getId(), true); - List instanceIds = new ArrayList(); - - for (LoadBalancerVMMapVO lbVmMap : lbVmMaps) { - instanceIds.add(lbVmMap.getInstanceId()); - } - - if (!instanceIds.isEmpty()) { - _lb2VmMapDao.remove(lb.getId(), instanceIds, null); - s_logger.debug("Load balancer rule id " + lb.getId() + " is removed for vms " + instanceIds); - } - - if (_lb2VmMapDao.listByLoadBalancerId(lb.getId()).isEmpty()) { - lb.setState(FirewallRule.State.Add); - _lbDao.persist(lb); - s_logger.debug("LB rule " + lb.getId() + " state is set to Add as there are no more active LB-VM mappings"); - } - - // remove LB-Stickiness policy mapping that were state to revoke - List stickinesspolicies = _lb2stickinesspoliciesDao.listByLoadBalancerId(lb.getId(), true); - if (!stickinesspolicies.isEmpty()) { - _lb2stickinesspoliciesDao.remove(lb.getId(), true); - s_logger.debug("Load balancer rule id " + lb.getId() + " is removed stickiness policies"); - } - - txn.commit(); - - if (checkForReleaseElasticIp) { - boolean success = true; - long count = _firewallDao.countRulesByIpId(lb.getSourceIpAddressId()); - if (count == 0) { - try { - success = handleSystemLBIpRelease(lb); - } catch (Exception ex) { - s_logger.warn("Failed to release system ip as a part of lb rule " + lb + " deletion due to exception ", ex); - success = false; - } finally { - if (!success) { - s_logger.warn("Failed to release system ip as a part of lb rule " + lb + " deletion"); - } - } - } - } - } - } - - return true; - } - - protected boolean handleSystemLBIpRelease(LoadBalancerVO lb) { - IpAddress ip = _ipAddressDao.findById(lb.getSourceIpAddressId()); - boolean success = true; - if (ip.getSystem()) { - s_logger.debug("Releasing system ip address " + lb.getSourceIpAddressId() + " as a part of delete lb rule"); - if (!_networkMgr.disassociatePublicIpAddress(lb.getSourceIpAddressId(), UserContext.current().getCallerUserId(), UserContext.current().getCaller())) { - s_logger.warn("Unable to release system ip address id=" + lb.getSourceIpAddressId() + " as a part of delete lb rule"); - success = false; - } else { - s_logger.warn("Successfully released system ip address id=" + lb.getSourceIpAddressId() + " as a part of delete lb rule"); - } - } - - return success; - } - - @Override - public boolean removeAllLoadBalanacersForIp(long ipId, Account caller, long callerUserId) { - List rules = _firewallDao.listByIpAndPurposeAndNotRevoked(ipId, Purpose.LoadBalancing); - if (rules != null) { - s_logger.debug("Found " + rules.size() + " lb rules to cleanup"); - } - for (FirewallRule rule : rules) { - boolean result = deleteLoadBalancerRule(rule.getId(), true, caller, callerUserId, false); - if (result == false) { - s_logger.warn("Unable to remove load balancer rule " + rule.getId()); - return false; - } - } - return true; - } - - @Override - public boolean removeAllLoadBalanacersForNetwork(long networkId, Account caller, long callerUserId) { - List rules = _firewallDao.listByNetworkAndPurposeAndNotRevoked(networkId, Purpose.LoadBalancing); - if (rules != null) { - s_logger.debug("Found " + rules.size() + " lb rules to cleanup"); - } - for (FirewallRule rule : rules) { - boolean result = deleteLoadBalancerRule(rule.getId(), true, caller, callerUserId, false); - if (result == false) { - s_logger.warn("Unable to remove load balancer rule " + rule.getId()); - return false; - } - } - return true; - } - - @Override - public List getStickinessPolicies(long lbId) { - List stickinessPolicies = new ArrayList(); - List sDbpolicies = _lb2stickinesspoliciesDao.listByLoadBalancerId(lbId); - - for (LBStickinessPolicyVO sDbPolicy : sDbpolicies) { - LbStickinessPolicy sPolicy = new LbStickinessPolicy(sDbPolicy.getMethodName(), sDbPolicy.getParams(), sDbPolicy.isRevoke()); - stickinessPolicies.add(sPolicy); - } - return stickinessPolicies; - } - - @Override - public List getExistingDestinations(long lbId) { - List dstList = new ArrayList(); - List lbVmMaps = _lb2VmMapDao.listByLoadBalancerId(lbId); - LoadBalancerVO lb = _lbDao.findById(lbId); - - String dstIp = null; - for (LoadBalancerVMMapVO lbVmMap : lbVmMaps) { - UserVm vm = _vmDao.findById(lbVmMap.getInstanceId()); - Nic nic = _nicDao.findByInstanceIdAndNetworkIdIncludingRemoved(lb.getNetworkId(), vm.getId()); - dstIp = nic.getIp4Address(); - LbDestination lbDst = new LbDestination(lb.getDefaultPortStart(), lb.getDefaultPortEnd(), dstIp, lbVmMap.isRevoke()); - dstList.add(lbDst); - } - return dstList; - } - - @Override - public boolean configure(String name, Map params) throws ConfigurationException { - _name = name; - return true; - } - - @Override - public boolean start() { - return true; - } - - @Override - public boolean stop() { - return true; - } - - @Override - public String getName() { - return _name; - } - - @Override - @ActionEvent(eventType = EventTypes.EVENT_LOAD_BALANCER_UPDATE, eventDescription = "updating load balancer", async = true) - public LoadBalancer updateLoadBalancerRule(UpdateLoadBalancerRuleCmd cmd) { - Account caller = UserContext.current().getCaller(); - Long lbRuleId = cmd.getId(); - String name = cmd.getLoadBalancerName(); - String description = cmd.getDescription(); - String algorithm = cmd.getAlgorithm(); - LoadBalancerVO lb = _lbDao.findById(lbRuleId); - LoadBalancerVO lbBackup = _lbDao.findById(lbRuleId); - - if (lb == null) { - throw new InvalidParameterValueException("Unable to find lb rule by id", null); - } - - // check permissions - _accountMgr.checkAccess(caller, null, true, lb); - - if (name != null) { - lb.setName(name); - } - - if (description != null) { - lb.setDescription(description); - } - - if (algorithm != null) { - lb.setAlgorithm(algorithm); - } - - boolean success = _lbDao.update(lbRuleId, lb); - - // If algorithm is changed, have to reapply the lb config - if (algorithm != null) { - try { - lb.setState(FirewallRule.State.Add); - _lbDao.persist(lb); - applyLoadBalancerConfig(lbRuleId); - } catch (ResourceUnavailableException e) { - if (isRollBackAllowedForProvider(lb)) { - /* - * NOTE : We use lb object to update db instead of lbBackup object since db layer will fail to - * update if there is no change in the object. - */ - if (lbBackup.getName() != null) { - lb.setName(lbBackup.getName()); - } - if (lbBackup.getDescription() != null) { - lb.setDescription(lbBackup.getDescription()); - } - if (lbBackup.getAlgorithm() != null) { - lb.setAlgorithm(lbBackup.getAlgorithm()); - } - lb.setState(lbBackup.getState()); - _lbDao.update(lb.getId(), lb); - _lbDao.persist(lb); - - s_logger.debug("LB Rollback rule id: " + lbRuleId + " while updating LB rule."); - } - s_logger.warn("Unable to apply the load balancer config because resource is unavaliable.", e); - success = false; - } - } - - if (!success) { - throw new CloudRuntimeException("Failed to update load balancer rule: " + lbRuleId); - } - - return lb; - } - - @Override - public List listLoadBalancerInstances(ListLoadBalancerRuleInstancesCmd cmd) throws PermissionDeniedException { - Account caller = UserContext.current().getCaller(); - Long loadBalancerId = cmd.getId(); - Boolean applied = cmd.isApplied(); - - if (applied == null) { - applied = Boolean.TRUE; - } - - LoadBalancerVO loadBalancer = _lbDao.findById(loadBalancerId); - if (loadBalancer == null) { - return null; - } - - _accountMgr.checkAccess(caller, null, true, loadBalancer); - - List loadBalancerInstances = new ArrayList(); - List vmLoadBalancerMappings = null; - - vmLoadBalancerMappings = _lb2VmMapDao.listByLoadBalancerId(loadBalancerId); - - List appliedInstanceIdList = new ArrayList(); - if ((vmLoadBalancerMappings != null) && !vmLoadBalancerMappings.isEmpty()) { - for (LoadBalancerVMMapVO vmLoadBalancerMapping : vmLoadBalancerMappings) { - appliedInstanceIdList.add(vmLoadBalancerMapping.getInstanceId()); - } - } - - IPAddressVO addr = _ipAddressDao.findById(loadBalancer.getSourceIpAddressId()); - List userVms = _vmDao.listVirtualNetworkInstancesByAcctAndZone(loadBalancer.getAccountId(), addr.getDataCenterId(), loadBalancer.getNetworkId()); - - for (UserVmVO userVm : userVms) { - // if the VM is destroyed, being expunged, in an error state, or in an unknown state, skip it - switch (userVm.getState()) { - case Destroyed: - case Expunging: - case Error: - case Unknown: - continue; - } - - boolean isApplied = appliedInstanceIdList.contains(userVm.getId()); - if ((isApplied && applied) || (!isApplied && !applied)) { - loadBalancerInstances.add(userVm); - } - } - - return loadBalancerInstances; - } - - public List getSupportedAutoScaleCounters(long networkid) - { - String capability = getLBCapability(networkid, Capability.AutoScaleCounters.getName()); - if (capability == null || capability.length() == 0) { - return null; - } - return Arrays.asList(capability.split(",")); - } - - @Override - public List getStickinessMethods(long networkid) - { - String capability = getLBCapability(networkid, Capability.SupportedStickinessMethods.getName()); - if (capability == null) { - return null; - } - Gson gson = new Gson(); - java.lang.reflect.Type listType = new TypeToken>() { - }.getType(); - List result = gson.fromJson(capability, listType); - return result; - } - - @Override - public List searchForLBStickinessPolicies(ListLBStickinessPoliciesCmd cmd) throws PermissionDeniedException { - Account caller = UserContext.current().getCaller(); - Long loadBalancerId = cmd.getLbRuleId(); - LoadBalancerVO loadBalancer = _lbDao.findById(loadBalancerId); - if (loadBalancer == null) { - return null; - } - - _accountMgr.checkAccess(caller, null, true, loadBalancer); - - List sDbpolicies = _lb2stickinesspoliciesDao.listByLoadBalancerId(cmd.getLbRuleId()); - - return sDbpolicies; - } - - @Override - public List searchForLoadBalancers(ListLoadBalancerRulesCmd cmd) { - Long ipId = cmd.getPublicIpId(); - Long zoneId = cmd.getZoneId(); - Long id = cmd.getId(); - String name = cmd.getLoadBalancerRuleName(); - String keyword = cmd.getKeyword(); - Long instanceId = cmd.getVirtualMachineId(); - Map tags = cmd.getTags(); - - Account caller = UserContext.current().getCaller(); - List permittedAccounts = new ArrayList(); - - Ternary domainIdRecursiveListProject = new Ternary(cmd.getDomainId(), cmd.isRecursive(), null); - _accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, cmd.listAll(), false); - Long domainId = domainIdRecursiveListProject.first(); - Boolean isRecursive = domainIdRecursiveListProject.second(); - ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third(); - - Filter searchFilter = new Filter(LoadBalancerVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal()); - SearchBuilder sb = _lbDao.createSearchBuilder(); - _accountMgr.buildACLSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria); - - sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ); - sb.and("name", sb.entity().getName(), SearchCriteria.Op.LIKE); - sb.and("sourceIpAddress", sb.entity().getSourceIpAddressId(), SearchCriteria.Op.EQ); - - if (instanceId != null) { - SearchBuilder lbVMSearch = _lb2VmMapDao.createSearchBuilder(); - lbVMSearch.and("instanceId", lbVMSearch.entity().getInstanceId(), SearchCriteria.Op.EQ); - sb.join("lbVMSearch", lbVMSearch, sb.entity().getId(), lbVMSearch.entity().getLoadBalancerId(), JoinBuilder.JoinType.INNER); - } - - if (zoneId != null) { - SearchBuilder ipSearch = _ipAddressDao.createSearchBuilder(); - ipSearch.and("zoneId", ipSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ); - sb.join("ipSearch", ipSearch, sb.entity().getSourceIpAddressId(), ipSearch.entity().getId(), JoinBuilder.JoinType.INNER); - } - - if (tags != null && !tags.isEmpty()) { - SearchBuilder tagSearch = _resourceTagDao.createSearchBuilder(); - for (int count = 0; count < tags.size(); count++) { - tagSearch.or().op("key" + String.valueOf(count), tagSearch.entity().getKey(), SearchCriteria.Op.EQ); - tagSearch.and("value" + String.valueOf(count), tagSearch.entity().getValue(), SearchCriteria.Op.EQ); - tagSearch.cp(); - } - tagSearch.and("resourceType", tagSearch.entity().getResourceType(), SearchCriteria.Op.EQ); - sb.groupBy(sb.entity().getId()); - sb.join("tagSearch", tagSearch, sb.entity().getId(), tagSearch.entity().getResourceId(), JoinBuilder.JoinType.INNER); - } - - SearchCriteria sc = sb.create(); - _accountMgr.buildACLSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria); - - if (keyword != null) { - SearchCriteria ssc = _lbDao.createSearchCriteria(); - ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%"); - ssc.addOr("description", SearchCriteria.Op.LIKE, "%" + keyword + "%"); - sc.addAnd("name", SearchCriteria.Op.SC, ssc); - } - - if (name != null) { - sc.setParameters("name", "%" + name + "%"); - } - - if (id != null) { - sc.setParameters("id", id); - } - - if (ipId != null) { - sc.setParameters("sourceIpAddress", ipId); - } - - if (instanceId != null) { - sc.setJoinParameters("lbVMSearch", "instanceId", instanceId); - } - - if (zoneId != null) { - sc.setJoinParameters("ipSearch", "zoneId", zoneId); - } - - if (tags != null && !tags.isEmpty()) { - int count = 0; - sc.setJoinParameters("tagSearch", "resourceType", TaggedResourceType.LoadBalancer.toString()); - for (String key : tags.keySet()) { - sc.setJoinParameters("tagSearch", "key" + String.valueOf(count), key); - sc.setJoinParameters("tagSearch", "value" + String.valueOf(count), tags.get(key)); - count++; - } - } - - return _lbDao.search(sc, searchFilter); - } - - @Override - public List listByNetworkId(long networkId) { - List lbs = _lbDao.listByNetworkId(networkId); - List lbRules = new ArrayList(); - for (LoadBalancerVO lb : lbs) { - List dstList = getExistingDestinations(lb.getId()); - List policyList = this.getStickinessPolicies(lb.getId()); - LoadBalancingRule loadBalancing = new LoadBalancingRule(lb, dstList, policyList); - lbRules.add(loadBalancing); - } - return lbRules; - } - - @Override - public LoadBalancerVO findById(long lbId) { - return _lbDao.findById(lbId); - } - - protected void removeLBRule(LoadBalancerVO rule) { - - //remove the rule - _lbDao.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()); - _vpcMgr.unassignIPFromVpcNetwork(ip.getId(), rule.getNetworkId()); - - } -} +// 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.network.lb; + +import java.security.InvalidParameterException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.ejb.Local; +import javax.naming.ConfigurationException; + +import org.apache.log4j.Logger; + +import com.cloud.api.commands.CreateLBStickinessPolicyCmd; +import com.cloud.api.commands.CreateLoadBalancerRuleCmd; +import com.cloud.api.commands.ListLBStickinessPoliciesCmd; +import com.cloud.api.commands.ListLoadBalancerRuleInstancesCmd; +import com.cloud.api.commands.ListLoadBalancerRulesCmd; +import com.cloud.api.commands.UpdateLoadBalancerRuleCmd; +import com.cloud.api.response.ServiceResponse; +import com.cloud.configuration.Config; +import com.cloud.configuration.ConfigurationManager; +import com.cloud.configuration.dao.ConfigurationDao; +import com.cloud.dc.dao.DataCenterDao; +import com.cloud.dc.dao.VlanDao; +import com.cloud.domain.dao.DomainDao; +import com.cloud.event.ActionEvent; +import com.cloud.event.EventTypes; +import com.cloud.event.UsageEventVO; +import com.cloud.event.dao.EventDao; +import com.cloud.event.dao.UsageEventDao; +import com.cloud.exception.InsufficientAddressCapacityException; +import com.cloud.exception.InvalidParameterValueException; +import com.cloud.exception.NetworkRuleConflictException; +import com.cloud.exception.PermissionDeniedException; +import com.cloud.exception.ResourceUnavailableException; +import com.cloud.network.ExternalLoadBalancerUsageManager; +import com.cloud.network.IPAddressVO; +import com.cloud.network.IpAddress; +import com.cloud.network.LBStickinessPolicyVO; +import com.cloud.network.LoadBalancerVMMapVO; +import com.cloud.network.LoadBalancerVO; +import com.cloud.network.Network; +import com.cloud.network.Network.Capability; +import com.cloud.network.Network.Provider; +import com.cloud.network.Network.Service; +import com.cloud.network.NetworkManager; +import com.cloud.network.NetworkVO; +import com.cloud.network.as.AutoScalePolicy; +import com.cloud.network.as.AutoScalePolicyConditionMapVO; +import com.cloud.network.as.AutoScaleVmGroup; +import com.cloud.network.as.AutoScaleVmGroupPolicyMapVO; +import com.cloud.network.as.AutoScaleVmGroupVO; +import com.cloud.network.as.AutoScaleVmProfile; +import com.cloud.network.as.Condition; +import com.cloud.network.as.Counter; +import com.cloud.network.as.dao.AutoScalePolicyConditionMapDao; +import com.cloud.network.as.dao.AutoScalePolicyDao; +import com.cloud.network.as.dao.AutoScaleVmGroupDao; +import com.cloud.network.as.dao.AutoScaleVmGroupPolicyMapDao; +import com.cloud.network.as.dao.AutoScaleVmProfileDao; +import com.cloud.network.as.dao.ConditionDao; +import com.cloud.network.as.dao.CounterDao; +import com.cloud.network.dao.FirewallRulesCidrsDao; +import com.cloud.network.dao.FirewallRulesDao; +import com.cloud.network.dao.IPAddressDao; +import com.cloud.network.dao.LBStickinessPolicyDao; +import com.cloud.network.dao.LoadBalancerDao; +import com.cloud.network.dao.LoadBalancerVMMapDao; +import com.cloud.network.dao.NetworkDao; +import com.cloud.network.dao.NetworkServiceMapDao; +import com.cloud.network.lb.LoadBalancingRule.LbAutoScalePolicy; +import com.cloud.network.lb.LoadBalancingRule.LbAutoScaleVmGroup; +import com.cloud.network.lb.LoadBalancingRule.LbAutoScaleVmProfile; +import com.cloud.network.lb.LoadBalancingRule.LbCondition; +import com.cloud.network.lb.LoadBalancingRule.LbDestination; +import com.cloud.network.lb.LoadBalancingRule.LbStickinessPolicy; +import com.cloud.network.rules.FirewallManager; +import com.cloud.network.rules.FirewallRule; +import com.cloud.network.rules.FirewallRule.FirewallRuleType; +import com.cloud.network.rules.FirewallRule.Purpose; +import com.cloud.network.rules.FirewallRuleVO; +import com.cloud.network.rules.LbStickinessMethod; +import com.cloud.network.rules.LbStickinessMethod.LbStickinessMethodParam; +import com.cloud.network.rules.LoadBalancer; +import com.cloud.network.rules.RulesManager; +import com.cloud.network.rules.StickinessPolicy; +import com.cloud.network.vpc.VpcManager; +import com.cloud.offering.NetworkOffering; +import com.cloud.projects.Project.ListProjectResourcesCriteria; +import com.cloud.server.ResourceTag.TaggedResourceType; +import com.cloud.tags.ResourceTagVO; +import com.cloud.tags.dao.ResourceTagDao; +import com.cloud.template.TemplateManager; +import com.cloud.user.Account; +import com.cloud.user.AccountManager; +import com.cloud.user.DomainService; +import com.cloud.user.User; +import com.cloud.user.UserContext; +import com.cloud.user.dao.AccountDao; +import com.cloud.user.dao.UserDao; +import com.cloud.uservm.UserVm; +import com.cloud.utils.IdentityProxy; +import com.cloud.utils.Ternary; +import com.cloud.utils.component.Inject; +import com.cloud.utils.component.Manager; +import com.cloud.utils.db.DB; +import com.cloud.utils.db.Filter; +import com.cloud.utils.db.JoinBuilder; +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.net.NetUtils; +import com.cloud.vm.Nic; +import com.cloud.vm.UserVmVO; +import com.cloud.vm.VirtualMachine.State; +import com.cloud.vm.dao.NicDao; +import com.cloud.vm.dao.UserVmDao; +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; + +@Local(value = { LoadBalancingRulesManager.class, LoadBalancingRulesService.class }) +public class LoadBalancingRulesManagerImpl implements LoadBalancingRulesManager, LoadBalancingRulesService, Manager { + private static final Logger s_logger = Logger.getLogger(LoadBalancingRulesManagerImpl.class); + + String _name; + + @Inject + NetworkManager _networkMgr; + @Inject + RulesManager _rulesMgr; + @Inject + AccountManager _accountMgr; + @Inject + IPAddressDao _ipAddressDao; + @Inject + LoadBalancerDao _lbDao; + @Inject + VlanDao _vlanDao; + @Inject + EventDao _eventDao; + @Inject + LoadBalancerVMMapDao _lb2VmMapDao; + @Inject + LBStickinessPolicyDao _lb2stickinesspoliciesDao; + @Inject + UserVmDao _vmDao; + @Inject + AccountDao _accountDao; + @Inject + DomainDao _domainDao; + @Inject + NicDao _nicDao; + @Inject + UsageEventDao _usageEventDao; + @Inject + FirewallRulesCidrsDao _firewallCidrsDao; + @Inject + FirewallManager _firewallMgr; + @Inject + ElasticLoadBalancerManager _elbMgr; + @Inject + NetworkDao _networkDao; + @Inject + FirewallRulesDao _firewallDao; + @Inject + DomainService _domainMgr; + @Inject + ConfigurationManager _configMgr; + @Inject + TemplateManager _templateMgr; + @Inject + ExternalLoadBalancerUsageManager _externalLBUsageMgr; + @Inject + NetworkServiceMapDao _ntwkSrvcDao; + @Inject + ResourceTagDao _resourceTagDao; + @Inject + CounterDao _counterDao; + @Inject + ConditionDao _conditionDao; + @Inject + AutoScaleVmProfileDao _autoScaleVmProfileDao; + @Inject + AutoScalePolicyDao _autoScalePolicyDao; + @Inject + AutoScalePolicyConditionMapDao _autoScalePolicyConditionMapDao; + @Inject + AutoScaleVmGroupDao _autoScaleVmGroupDao; + @Inject + AutoScaleVmGroupPolicyMapDao _autoScaleVmGroupPolicyMapDao; + @Inject + ConfigurationDao _configDao; + @Inject + DataCenterDao _dcDao = null; + @Inject + UserDao _userDao; + @Inject + VpcManager _vpcMgr; + + + // Will return a string. For LB Stickiness this will be a json, for autoscale this will be "," separated values + @Override + public String getLBCapability(long networkid, String capabilityName) { + Map> serviceCapabilitiesMap = _networkMgr.getNetworkCapabilities(networkid); + if (serviceCapabilitiesMap != null) { + for (Service service : serviceCapabilitiesMap.keySet()) { + ServiceResponse serviceResponse = new ServiceResponse(); + serviceResponse.setName(service.getName()); + if ("Lb".equalsIgnoreCase(service.getName())) { + Map serviceCapabilities = serviceCapabilitiesMap + .get(service); + if (serviceCapabilities != null) { + for (Capability capability : serviceCapabilities + .keySet()) { + if (capabilityName.equals(capability.getName())) { + return serviceCapabilities.get(capability); + } + } + } + } + } + } + return null; + } + + private LbAutoScaleVmGroup getLbAutoScaleVmGroup(AutoScaleVmGroup vmGroup) { + List vmGroupPolicyMapList = _autoScaleVmGroupPolicyMapDao.listByVmGroupId(vmGroup.getId()); + List autoScalePolicies = new ArrayList(); + for (AutoScaleVmGroupPolicyMapVO vmGroupPolicyMap : vmGroupPolicyMapList) { + AutoScalePolicy autoScalePolicy = _autoScalePolicyDao.findById(vmGroupPolicyMap.getPolicyId()); + List autoScalePolicyConditionMapList = _autoScalePolicyConditionMapDao.listByAll(autoScalePolicy.getId(), null); + List lbConditions = new ArrayList(); + for (AutoScalePolicyConditionMapVO autoScalePolicyConditionMap : autoScalePolicyConditionMapList) { + Condition condition = _conditionDao.findById(autoScalePolicyConditionMap.getConditionId()); + Counter counter = _counterDao.findById(condition.getCounterid()); + lbConditions.add(new LbCondition(counter, condition)); + } + autoScalePolicies.add(new LbAutoScalePolicy(autoScalePolicy, lbConditions)); + } + AutoScaleVmProfile autoScaleVmProfile = _autoScaleVmProfileDao.findById(vmGroup.getProfileId()); + Long autoscaleUserId = autoScaleVmProfile.getAutoScaleUserId(); + User user = _userDao.findById(autoscaleUserId); + String apiKey = user.getApiKey(); + String secretKey = user.getSecretKey(); + String csUrl = _configDao.getValue(Config.EndpointeUrl.key()); + + if(apiKey == null) { + 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", 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", null); + } + + LbAutoScaleVmProfile lbAutoScaleVmProfile = new LbAutoScaleVmProfile(autoScaleVmProfile, apiKey, secretKey, csUrl); + return new LbAutoScaleVmGroup(vmGroup, autoScalePolicies, lbAutoScaleVmProfile); + } + + private boolean applyAutoScaleConfig(LoadBalancerVO lb, AutoScaleVmGroupVO vmGroup) throws ResourceUnavailableException { + LbAutoScaleVmGroup lbAutoScaleVmGroup = getLbAutoScaleVmGroup(vmGroup); + /* Regular config like destinations need not be packed for applying autoscale config as of today.*/ + LoadBalancingRule rule = new LoadBalancingRule(lb, null, null); + rule.setAutoScaleVmGroup(lbAutoScaleVmGroup); + + if (!isRollBackAllowedForProvider(lb)) { + // this is for Netscalar type of devices. if their is failure the db entries will be rollbacked. + return false; + } + + List rules = Arrays.asList(rule); + + if (!_networkMgr.applyRules(rules, false)) { + s_logger.debug("LB rules' autoscale config are not completely applied"); + return false; + } + + return true; + } + + @Override + @DB + public boolean configureLbAutoScaleVmGroup(long vmGroupid) { + AutoScaleVmGroupVO vmGroup = _autoScaleVmGroupDao.findById(vmGroupid); + boolean success = false; + + LoadBalancerVO loadBalancer = _lbDao.findById(vmGroup.getLoadBalancerId()); + + FirewallRule.State backupState = loadBalancer.getState(); + + if (vmGroup.getState().equals(AutoScaleVmGroup.State_New)) { + loadBalancer.setState(FirewallRule.State.Add); + _lbDao.persist(loadBalancer); + } + else if (loadBalancer.getState() == FirewallRule.State.Active && + vmGroup.getState().equals(AutoScaleVmGroup.State_Revoke)) { + loadBalancer.setState(FirewallRule.State.Add); + _lbDao.persist(loadBalancer); + } + + try { + success = applyAutoScaleConfig(loadBalancer, vmGroup); + } catch (ResourceUnavailableException e) { + s_logger.warn("Unable to configure AutoScaleVmGroup to the lb rule: " + loadBalancer.getId() + " because resource is unavaliable:", e); + } finally { + if (!success) { + s_logger.warn("Failed to configure LB Auto Scale Vm Group with Id:" + vmGroupid); + if (isRollBackAllowedForProvider(loadBalancer)) { + loadBalancer.setState(backupState); + _lbDao.persist(loadBalancer); + s_logger.debug("LB Rollback rule id: " + loadBalancer.getId() + " lb state rolback while creating AutoscaleVmGroup"); + } + } + } + + if (success) { + if (vmGroup.getState().equals(AutoScaleVmGroup.State_New)) { + Transaction.currentTxn().start(); + loadBalancer.setState(FirewallRule.State.Active); + s_logger.debug("LB rule " + loadBalancer.getId() + " state is set to Active"); + _lbDao.persist(loadBalancer); + vmGroup.setState(AutoScaleVmGroup.State_Enabled); + _autoScaleVmGroupDao.persist(vmGroup); + s_logger.debug("LB Auto Scale Vm Group with Id: " + vmGroupid + " is set to Enabled state."); + Transaction.currentTxn().commit(); + } + s_logger.info("Successfully configured LB Autoscale Vm Group with Id: " + vmGroupid); + } + return success; + } + + private boolean genericValidator(CreateLBStickinessPolicyCmd cmd) throws InvalidParameterValueException { + LoadBalancerVO loadBalancer = _lbDao.findById(cmd.getLbRuleId()); + /* Validation : check for valid Method name and params */ + List stickinessMethodList = getStickinessMethods(loadBalancer + .getNetworkId()); + boolean methodMatch = false; + + if (stickinessMethodList == null) { + List idList = new ArrayList(); + idList.add(new IdentityProxy("firewall_rules", cmd.getLbRuleId(), "ruleId")); + throw new InvalidParameterValueException("Failed: No Stickiness method available for LB rule with specified id", idList); + } + for (LbStickinessMethod method : stickinessMethodList) { + if (method.getMethodName().equalsIgnoreCase(cmd.getStickinessMethodName())) { + methodMatch = true; + Map apiParamList = cmd.getparamList(); + List methodParamList = method.getParamList(); + Map tempParamList = new HashMap(); + + /* + * validation-1: check for any extra params that are not + * required by the policymethod(capability), FIXME: make the + * below loop simple without using raw data type + */ + if (apiParamList != null) { + Collection userGroupCollection = apiParamList.values(); + Iterator iter = userGroupCollection.iterator(); + while (iter.hasNext()) { + HashMap paramKVpair = (HashMap) iter.next(); + String paramName = paramKVpair.get("name"); + String paramValue = paramKVpair.get("value"); + + tempParamList.put(paramName, paramValue); + Boolean found = false; + for (LbStickinessMethodParam param : methodParamList) { + if (param.getParamName().equalsIgnoreCase(paramName)) { + if ((param.getIsflag() == false) && (paramValue == null)) { + throw new InvalidParameterValueException("Failed : Value expected for the Param :" + param.getParamName(), null); + } + found = true; + break; + } + } + if (!found) { + throw new InvalidParameterValueException("Failed : Stickiness policy does not support param name :" + paramName, null); + } + } + } + + /* validation-2: check for mandatory params */ + for (LbStickinessMethodParam param : methodParamList) { + if (param.getRequired()) { + if (tempParamList.get(param.getParamName()) == null) { + throw new InvalidParameterValueException("Failed : Missing Manadatory Param :" + param.getParamName(), null); + } + } + } + /* Successfully completed the Validation */ + break; + } + } + if (methodMatch == false) { + List idList = new ArrayList(); + idList.add(new IdentityProxy("firewall_rules", cmd.getLbRuleId(), "ruleId")); + throw new InvalidParameterValueException("Failed to match Stickiness method name for LB rule whose id is specified", idList); + } + + /* Validation : check for the multiple policies to the rule id */ + List stickinessPolicies = _lb2stickinesspoliciesDao.listByLoadBalancerId(cmd.getLbRuleId(), false); + if (stickinessPolicies.size() > 0) { + List idList = new ArrayList(); + idList.add(new IdentityProxy("firewall_rules", cmd.getLbRuleId(), "ruleId")); + throw new InvalidParameterValueException("Failed to create Stickiness policy: Policy already attached", idList); + } + return true; + } + + @SuppressWarnings("rawtypes") + @Override + @DB + @ActionEvent(eventType = EventTypes.EVENT_LB_STICKINESSPOLICY_CREATE, eventDescription = "create lb stickinesspolicy to load balancer", create = true) + public StickinessPolicy createLBStickinessPolicy(CreateLBStickinessPolicyCmd cmd) throws NetworkRuleConflictException { + UserContext caller = UserContext.current(); + + /* Validation : check corresponding load balancer rule exist */ + LoadBalancerVO loadBalancer = _lbDao.findById(cmd.getLbRuleId()); + if (loadBalancer == null) { + throw new InvalidParameterValueException("Failed: LB rule provided not present", null); + } + + _accountMgr.checkAccess(caller.getCaller(), null, true, loadBalancer); + if (loadBalancer.getState() == FirewallRule.State.Revoke) { + List idList = new ArrayList(); + idList.add(new IdentityProxy("firewall_rules", cmd.getLbRuleId(), "ruleId")); + throw new InvalidParameterValueException("Failed: LB rule with specified id is in deleting state: ", idList); + } + + /* Generic validations */ + if (!genericValidator(cmd)) { + List idList = new ArrayList(); + idList.add(new IdentityProxy("firewall_rules", cmd.getLbRuleId(), "ruleId")); + throw new InvalidParameterValueException("Failed to create Stickiness policy: Validation of rule with specified id failed", idList); + } + + /* Specific validations using network element validator for specific validations */ + LBStickinessPolicyVO lbpolicy = new LBStickinessPolicyVO(loadBalancer.getId(), cmd.getLBStickinessPolicyName(), cmd.getStickinessMethodName(), cmd.getparamList(), cmd.getDescription()); + List policyList = new ArrayList(); + policyList.add(new LbStickinessPolicy(cmd.getStickinessMethodName(), lbpolicy.getParams())); + LoadBalancingRule lbRule = new LoadBalancingRule(loadBalancer, getExistingDestinations(lbpolicy.getId()), policyList); + if (!_networkMgr.validateRule(lbRule)) { + List idList = new ArrayList(); + idList.add(new IdentityProxy("firewall_rules", cmd.getLbRuleId(), "ruleId")); + throw new InvalidParameterValueException("Failed to create Stickiness policy: Validation of rule with specified id failed ", idList); + } + + /* Finally Insert into DB */ + LBStickinessPolicyVO policy = new LBStickinessPolicyVO(loadBalancer.getId(), cmd.getLBStickinessPolicyName(), cmd.getStickinessMethodName(), cmd.getparamList(), cmd.getDescription()); + policy = _lb2stickinesspoliciesDao.persist(policy); + + return policy; + } + + @Override + @DB + @ActionEvent(eventType = EventTypes.EVENT_LB_STICKINESSPOLICY_CREATE, eventDescription = "Apply Stickinesspolicy to load balancer ", async = true) + public boolean applyLBStickinessPolicy(CreateLBStickinessPolicyCmd cmd) { + boolean success = true; + + LoadBalancerVO loadBalancer = _lbDao.findById(cmd.getLbRuleId()); + if (loadBalancer == null) { + throw new InvalidParameterException("Invalid Load balancer Id:" + cmd.getLbRuleId()); + } + FirewallRule.State backupState = loadBalancer.getState(); + loadBalancer.setState(FirewallRule.State.Add); + _lbDao.persist(loadBalancer); + try { + applyLoadBalancerConfig(cmd.getLbRuleId()); + } catch (ResourceUnavailableException e) { + s_logger.warn("Unable to apply Stickiness policy to the lb rule: " + cmd.getLbRuleId() + " because resource is unavaliable:", e); + if (isRollBackAllowedForProvider(loadBalancer)) { + loadBalancer.setState(backupState); + _lbDao.persist(loadBalancer); + s_logger.debug("LB Rollback rule id: " + loadBalancer.getId() + " lb state rolback while creating sticky policy"); + } + deleteLBStickinessPolicy(cmd.getEntityId(), false); + success = false; + } + + return success; + } + + @Override + @ActionEvent(eventType = EventTypes.EVENT_LB_STICKINESSPOLICY_DELETE, eventDescription = "revoking LB Stickiness policy ", async = true) + public boolean deleteLBStickinessPolicy(long stickinessPolicyId, boolean apply) { + boolean success = true; + + UserContext caller = UserContext.current(); + LBStickinessPolicyVO stickinessPolicy = _lb2stickinesspoliciesDao.findById(stickinessPolicyId); + + if (stickinessPolicy == null) { + throw new InvalidParameterException("Invalid Stickiness policy id value: " + stickinessPolicyId); + } + LoadBalancerVO loadBalancer = _lbDao.findById(Long.valueOf(stickinessPolicy.getLoadBalancerId())); + if (loadBalancer == null) { + throw new InvalidParameterException("Invalid Load balancer : " + stickinessPolicy.getLoadBalancerId() + " for Stickiness policy id: " + stickinessPolicyId); + } + long loadBalancerId = loadBalancer.getId(); + FirewallRule.State backupState = loadBalancer.getState(); + _accountMgr.checkAccess(caller.getCaller(), null, true, loadBalancer); + + if (apply) { + if (loadBalancer.getState() == FirewallRule.State.Active) { + loadBalancer.setState(FirewallRule.State.Add); + _lbDao.persist(loadBalancer); + } + + boolean backupStickyState = stickinessPolicy.isRevoke(); + stickinessPolicy.setRevoke(true); + _lb2stickinesspoliciesDao.persist(stickinessPolicy); + s_logger.debug("Set load balancer rule for revoke: rule id " + loadBalancerId + ", stickinesspolicyID " + stickinessPolicyId); + + try { + if (!applyLoadBalancerConfig(loadBalancerId)) { + s_logger.warn("Failed to remove load balancer rule id " + loadBalancerId + " for stickinesspolicyID " + stickinessPolicyId); + throw new CloudRuntimeException("Failed to remove load balancer rule id " + loadBalancerId + " for stickinesspolicyID " + stickinessPolicyId); + } + } catch (ResourceUnavailableException e) { + if (isRollBackAllowedForProvider(loadBalancer)) { + stickinessPolicy.setRevoke(backupStickyState); + _lb2stickinesspoliciesDao.persist(stickinessPolicy); + loadBalancer.setState(backupState); + _lbDao.persist(loadBalancer); + s_logger.debug("LB Rollback rule id: " + loadBalancer.getId() + " while deleting sticky policy: " + stickinessPolicyId); + } + s_logger.warn("Unable to apply the load balancer config because resource is unavaliable.", e); + success = false; + } + } else { + _lb2stickinesspoliciesDao.remove(stickinessPolicy.getLoadBalancerId()); + } + + return success; + } + + private boolean isRollBackAllowedForProvider(LoadBalancerVO loadBalancer) { + Network network = _networkDao.findById(loadBalancer.getNetworkId()); + Provider provider = Network.Provider.Netscaler; + return _ntwkSrvcDao.canProviderSupportServiceInNetwork(network.getId(), Service.Lb, provider); + } + + @Override + @DB + @ActionEvent(eventType = EventTypes.EVENT_ASSIGN_TO_LOAD_BALANCER_RULE, eventDescription = "assigning to load balancer", async = true) + public boolean assignToLoadBalancer(long loadBalancerId, List instanceIds) { + UserContext ctx = UserContext.current(); + Account caller = ctx.getCaller(); + + LoadBalancerVO loadBalancer = _lbDao.findById(loadBalancerId); + if (loadBalancer == null) { + throw new InvalidParameterValueException("Failed to assign to load balancer; the load balancer was not found.", null); + } + + List mappedInstances = _lb2VmMapDao.listByLoadBalancerId(loadBalancerId, false); + Set mappedInstanceIds = new HashSet(); + for (LoadBalancerVMMapVO mappedInstance : mappedInstances) { + mappedInstanceIds.add(Long.valueOf(mappedInstance.getInstanceId())); + } + + List vmsToAdd = new ArrayList(); + + for (Long instanceId : instanceIds) { + if (mappedInstanceIds.contains(instanceId)) { + List idList = new ArrayList(); + idList.add(new IdentityProxy("user_vm", instanceId, "vmId")); + throw new InvalidParameterValueException("VM with specified id is already mapped to load balancer", idList); + } + + UserVm vm = _vmDao.findById(instanceId); + if (vm == null || vm.getState() == State.Destroyed || vm.getState() == State.Expunging) { + throw new InvalidParameterValueException("Couldn't locate vm instance by id", null); + } + + _rulesMgr.checkRuleAndUserVm(loadBalancer, vm, caller); + + if (vm.getAccountId() != loadBalancer.getAccountId()) { + throw new PermissionDeniedException("Cannot add virtual machines that do not belong to the same owner."); + } + + // Let's check to make sure the vm has a nic in the same network as the load balancing rule. + List nics = _networkMgr.getNics(vm.getId()); + Nic nicInSameNetwork = null; + for (Nic nic : nics) { + if (nic.getNetworkId() == loadBalancer.getNetworkId()) { + nicInSameNetwork = nic; + break; + } + } + + if (nicInSameNetwork == null) { + List idList = new ArrayList(); + idList.add(new IdentityProxy(vm, instanceId, "vmId")); + throw new InvalidParameterValueException("VM with specified id cannot be added because it doesn't belong in the same network.", idList); + + } + + if (s_logger.isDebugEnabled()) { + s_logger.debug("Adding " + vm + " to the load balancer pool"); + } + vmsToAdd.add(vm); + } + + Transaction txn = Transaction.currentTxn(); + txn.start(); + for (UserVm vm : vmsToAdd) { + LoadBalancerVMMapVO map = new LoadBalancerVMMapVO(loadBalancer.getId(), vm.getId(), false); + map = _lb2VmMapDao.persist(map); + } + txn.commit(); + + if (_autoScaleVmGroupDao.isAutoScaleLoadBalancer(loadBalancerId)) { + // Nothing needs to be done for an autoscaled loadbalancer, + // just persist and proceed. + return true; + } + + boolean success = false; + FirewallRule.State backupState = loadBalancer.getState(); + try { + loadBalancer.setState(FirewallRule.State.Add); + _lbDao.persist(loadBalancer); + applyLoadBalancerConfig(loadBalancerId); + success = true; + } catch (ResourceUnavailableException e) { + if (isRollBackAllowedForProvider(loadBalancer)) { + List vmInstanceIds = new ArrayList(); + txn = Transaction.currentTxn(); + txn.start(); + for (UserVm vm : vmsToAdd) { + vmInstanceIds.add(vm.getId()); + } + txn.commit(); + if (!vmInstanceIds.isEmpty()) { + _lb2VmMapDao.remove(loadBalancer.getId(), vmInstanceIds, null); + s_logger.debug("LB Rollback rule id: " + loadBalancer.getId() + " while attaching VM: " + vmInstanceIds); + } + loadBalancer.setState(backupState); + _lbDao.persist(loadBalancer); + } + s_logger.warn("Unable to apply the load balancer config because resource is unavaliable.", e); + } + + if (!success) { + CloudRuntimeException ex = new CloudRuntimeException("Failed to add specified loadbalancerruleid for vms " + instanceIds); + ex.addProxyObject(loadBalancer, loadBalancerId, "loadBalancerId"); + // TBD: Also pack in the instanceIds in the exception using the right VO object or table name. + throw ex; + } + + return success; + } + + @Override + @ActionEvent(eventType = EventTypes.EVENT_REMOVE_FROM_LOAD_BALANCER_RULE, eventDescription = "removing from load balancer", async = true) + public boolean removeFromLoadBalancer(long loadBalancerId, List instanceIds) { + return removeFromLoadBalancerInternal(loadBalancerId, instanceIds, true); + } + + private boolean removeFromLoadBalancerInternal(long loadBalancerId, List instanceIds, boolean rollBack) { + UserContext caller = UserContext.current(); + + LoadBalancerVO loadBalancer = _lbDao.findById(Long.valueOf(loadBalancerId)); + if (loadBalancer == null) { + throw new InvalidParameterException("Invalid load balancer value: " + loadBalancerId); + } + + _accountMgr.checkAccess(caller.getCaller(), null, true, loadBalancer); + + boolean success = false; + FirewallRule.State backupState = loadBalancer.getState(); + try { + loadBalancer.setState(FirewallRule.State.Add); + _lbDao.persist(loadBalancer); + + for (long instanceId : instanceIds) { + LoadBalancerVMMapVO map = _lb2VmMapDao.findByLoadBalancerIdAndVmId(loadBalancerId, instanceId); + map.setRevoke(true); + _lb2VmMapDao.persist(map); + s_logger.debug("Set load balancer rule for revoke: rule id " + loadBalancerId + ", vmId " + instanceId); + } + + if (_autoScaleVmGroupDao.isAutoScaleLoadBalancer(loadBalancerId)) { + // Nothing needs to be done for an autoscaled loadbalancer, + // just persist and proceed. + return true; + } + + if (!applyLoadBalancerConfig(loadBalancerId)) { + s_logger.warn("Failed to remove load balancer rule id " + loadBalancerId + " for vms " + instanceIds); + CloudRuntimeException ex = new CloudRuntimeException("Failed to remove specified load balancer rule id for vms " + instanceIds); + ex.addProxyObject(loadBalancer, loadBalancerId, "loadBalancerId"); + throw ex; + } + success = true; + } catch (ResourceUnavailableException e) { + if (rollBack && isRollBackAllowedForProvider(loadBalancer)) { + + for (long instanceId : instanceIds) { + LoadBalancerVMMapVO map = _lb2VmMapDao.findByLoadBalancerIdAndVmId(loadBalancerId, instanceId); + map.setRevoke(false); + _lb2VmMapDao.persist(map); + s_logger.debug("LB Rollback rule id: " + loadBalancerId + ",while removing vmId " + instanceId); + } + + loadBalancer.setState(backupState); + _lbDao.persist(loadBalancer); + s_logger.debug("LB Rollback rule id: " + loadBalancerId + " while removing vm instances"); + } + s_logger.warn("Unable to apply the load balancer config because resource is unavaliable.", e); + } + if (!success) { + CloudRuntimeException ex = new CloudRuntimeException("Failed to remove specified load balancer rule id for vms " + instanceIds); + ex.addProxyObject(loadBalancer, loadBalancerId, "loadBalancerId"); + throw ex; + } + return success; + } + + @Override + public boolean removeVmFromLoadBalancers(long instanceId) { + boolean success = true; + List maps = _lb2VmMapDao.listByInstanceId(instanceId); + if (maps == null || maps.isEmpty()) { + return true; + } + + Map> lbsToReconfigure = new HashMap>(); + + // first set all existing lb mappings with Revoke state + for (LoadBalancerVMMapVO map : maps) { + long lbId = map.getLoadBalancerId(); + List instances = lbsToReconfigure.get(lbId); + if (instances == null) { + instances = new ArrayList(); + } + instances.add(map.getInstanceId()); + lbsToReconfigure.put(lbId, instances); + + map.setRevoke(true); + _lb2VmMapDao.persist(map); + s_logger.debug("Set load balancer rule for revoke: rule id " + map.getLoadBalancerId() + ", vmId " + instanceId); + } + + // Reapply all lbs that had the vm assigned + if (lbsToReconfigure != null) { + for (Map.Entry> lb : lbsToReconfigure.entrySet()) { + if (!removeFromLoadBalancerInternal(lb.getKey(), lb.getValue(), false)) { + success = false; + } + } + } + return success; + } + + @Override + @ActionEvent(eventType = EventTypes.EVENT_LOAD_BALANCER_DELETE, eventDescription = "deleting load balancer", async = true) + public boolean deleteLoadBalancerRule(long loadBalancerId, boolean apply) { + UserContext ctx = UserContext.current(); + Account caller = ctx.getCaller(); + + LoadBalancerVO rule = _lbDao.findById(loadBalancerId); + if (rule == null) { + throw new InvalidParameterValueException("Unable to find load balancer rule by id", null); + } + + _accountMgr.checkAccess(caller, null, true, rule); + + boolean result = deleteLoadBalancerRule(loadBalancerId, apply, caller, ctx.getCallerUserId(), true); + if (!result) { + throw new CloudRuntimeException("Unable to remove load balancer rule " + loadBalancerId); + } + return result; + } + + @DB + public boolean deleteLoadBalancerRule(long loadBalancerId, boolean apply, Account caller, long callerUserId, boolean rollBack) { + LoadBalancerVO lb = _lbDao.findById(loadBalancerId); + Transaction txn = Transaction.currentTxn(); + boolean generateUsageEvent = false; + boolean success = true; + FirewallRule.State backupState = lb.getState(); + + txn.start(); + if (lb.getState() == FirewallRule.State.Staged) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Found a rule that is still in stage state so just removing it: " + lb); + } + generateUsageEvent = true; + } else if (lb.getState() == FirewallRule.State.Add || lb.getState() == FirewallRule.State.Active) { + lb.setState(FirewallRule.State.Revoke); + _lbDao.persist(lb); + generateUsageEvent = true; + } + List backupMaps = _lb2VmMapDao.listByLoadBalancerId(loadBalancerId); + List maps = _lb2VmMapDao.listByLoadBalancerId(loadBalancerId); + if (maps != null) { + for (LoadBalancerVMMapVO map : maps) { + map.setRevoke(true); + _lb2VmMapDao.persist(map); + s_logger.debug("Set load balancer rule for revoke: rule id " + loadBalancerId + ", vmId " + map.getInstanceId()); + } + } + + if (generateUsageEvent) { + // Generate usage event right after all rules were marked for revoke + UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_LOAD_BALANCER_DELETE, lb.getAccountId(), 0, lb.getId(), null); + _usageEventDao.persist(usageEvent); + } + + txn.commit(); + + // gather external network usage stats for this lb rule + NetworkVO network = _networkDao.findById(lb.getNetworkId()); + if (network != null) { + if (_networkMgr.networkIsConfiguredForExternalNetworking(network.getDataCenterId(), network.getId())) { + _externalLBUsageMgr.updateExternalLoadBalancerNetworkUsageStats(loadBalancerId); + } + } + + if (apply) { + try { + if (_autoScaleVmGroupDao.isAutoScaleLoadBalancer(loadBalancerId)) { + // Get the associated VmGroup + AutoScaleVmGroupVO vmGroup = _autoScaleVmGroupDao.listByAll(loadBalancerId, null).get(0); + if (!applyAutoScaleConfig(lb, vmGroup)) { + s_logger.warn("Unable to apply the autoscale config"); + return false; + } + } else { + if (!applyLoadBalancerConfig(loadBalancerId)) { + s_logger.warn("Unable to apply the load balancer config"); + return false; + } + } + } catch (ResourceUnavailableException e) { + if (rollBack && isRollBackAllowedForProvider(lb)) { + if (backupMaps != null) { + for (LoadBalancerVMMapVO map : backupMaps) { + _lb2VmMapDao.persist(map); + s_logger.debug("LB Rollback rule id: " + loadBalancerId + ", vmId " + map.getInstanceId()); + } + } + lb.setState(backupState); + _lbDao.persist(lb); + s_logger.debug("LB Rollback rule id: " + loadBalancerId + " while deleting LB rule."); + } else { + s_logger.warn("Unable to apply the load balancer config because resource is unavaliable.", e); + } + return false; + } + } + + FirewallRuleVO relatedRule = _firewallDao.findByRelatedId(lb.getId()); + if (relatedRule != null) { + s_logger.warn("Unable to remove firewall rule id=" + lb.getId() + " as it has related firewall rule id=" + relatedRule.getId() + "; leaving it in Revoke state"); + success = false; + } else { + _firewallMgr.removeRule(lb); + } + + _elbMgr.handleDeleteLoadBalancerRule(lb, callerUserId, caller); + + if (success) { + s_logger.debug("Load balancer with id " + lb.getId() + " is removed successfully"); + } + + return success; + } + + @Override + @ActionEvent(eventType = EventTypes.EVENT_LOAD_BALANCER_CREATE, eventDescription = "creating load balancer") + public LoadBalancer createLoadBalancerRule(CreateLoadBalancerRuleCmd lb, boolean openFirewall) throws NetworkRuleConflictException, InsufficientAddressCapacityException { + Account lbOwner = _accountMgr.getAccount(lb.getEntityOwnerId()); + + int defPortStart = lb.getDefaultPortStart(); + int defPortEnd = lb.getDefaultPortEnd(); + + if (!NetUtils.isValidPort(defPortEnd)) { + throw new InvalidParameterValueException("privatePort is an invalid value: " + defPortEnd, null); + } + if (defPortStart > defPortEnd) { + throw new InvalidParameterValueException("private port range is invalid: " + defPortStart + "-" + defPortEnd, null); + } + if ((lb.getAlgorithm() == null) || !NetUtils.isValidAlgorithm(lb.getAlgorithm())) { + throw new InvalidParameterValueException("Invalid algorithm: " + lb.getAlgorithm(), null); + } + + Long ipAddrId = lb.getSourceIpAddressId(); + IPAddressVO ipVO = null; + if (ipAddrId != null) { + ipVO = _ipAddressDao.findById(ipAddrId); + } + + Network network = _networkMgr.getNetwork(lb.getNetworkId()); + + LoadBalancer result = _elbMgr.handleCreateLoadBalancerRule(lb, lbOwner, lb.getNetworkId()); + boolean performedIpAssoc = false; + if (result == null) { + IpAddress systemIp = null; + NetworkOffering off = _configMgr.getNetworkOffering(network.getNetworkOfferingId()); + if (off.getElasticLb() && ipVO == null && network.getVpcId() == null) { + systemIp = _networkMgr.assignSystemIp(lb.getNetworkId(), lbOwner, true, false); + lb.setSourceIpAddressId(systemIp.getId()); + ipVO = _ipAddressDao.findById(systemIp.getId()); + } + + // Validate ip address + if (ipVO == null) { + throw new InvalidParameterValueException("Unable to create load balance rule; can't find/allocate source IP", null); + } else if (ipVO.isOneToOneNat()) { + throw new NetworkRuleConflictException("Can't do load balance on ip address: " + ipVO.getAddress()); + } + + try { + if (ipVO.getAssociatedWithNetworkId() == null) { + boolean assignToVpcNtwk = network.getVpcId() != null + && ipVO.getVpcId() != null && ipVO.getVpcId().longValue() == network.getVpcId(); + if (assignToVpcNtwk) { + // set networkId just for verification purposes + _networkMgr.checkIpForService(ipVO, Service.Lb, lb.getNetworkId()); + + s_logger.debug("The ip is not associated with the VPC network id=" + lb.getNetworkId() + " so assigning"); + ipVO = _networkMgr.associateIPToGuestNetwork(ipAddrId, lb.getNetworkId(), false); + performedIpAssoc = true; + } + } else { + _networkMgr.checkIpForService(ipVO, Service.Lb, null); + } + + if (ipVO.getAssociatedWithNetworkId() == null) { + throw new InvalidParameterValueException("Ip address " + ipVO + " is not assigned to the network " + network, null); + } + + if (lb.getSourceIpAddressId() == null) { + throw new CloudRuntimeException("No ip address is defined to assign the LB to"); + } + result = createLoadBalancer(lb, openFirewall); + } catch (Exception ex) { + s_logger.warn("Failed to create load balancer due to ", ex); + if (ex instanceof NetworkRuleConflictException) { + throw (NetworkRuleConflictException) ex; + } + } finally { + if (result == null && systemIp != null) { + s_logger.debug("Releasing system IP address " + systemIp + " as corresponding lb rule failed to create"); + _networkMgr.handleSystemIpRelease(systemIp); + } + // release ip address if ipassoc was perfored + if (performedIpAssoc) { + ipVO = _ipAddressDao.findById(ipVO.getId()); + _vpcMgr.unassignIPFromVpcNetwork(ipVO.getId(), lb.getNetworkId()); + + } + } + } + + if (result == null) { + throw new CloudRuntimeException("Failed to create load balancer rule: " + lb.getName()); + } + + return result; + } + + @Override + @DB + public LoadBalancer createLoadBalancer(CreateLoadBalancerRuleCmd lb, boolean openFirewall) throws NetworkRuleConflictException { + UserContext caller = UserContext.current(); + int srcPortStart = lb.getSourcePortStart(); + int defPortStart = lb.getDefaultPortStart(); + int srcPortEnd = lb.getSourcePortEnd(); + long sourceIpId = lb.getSourceIpAddressId(); + + IPAddressVO ipAddr = _ipAddressDao.findById(sourceIpId); + // make sure ip address exists + if (ipAddr == null || !ipAddr.readyToUse()) { + throw new InvalidParameterValueException("Unable to create load balancer rule, invalid IP address id specified", null); + } else if (ipAddr.isOneToOneNat()) { + List idList = new ArrayList(); + idList.add(new IdentityProxy(ipAddr, sourceIpId, "sourceIpId")); + throw new InvalidParameterValueException("Unable to create load balancer rule; specified sourceip id has static nat enabled", idList); + } + + _firewallMgr.validateFirewallRule(caller.getCaller(), ipAddr, srcPortStart, srcPortEnd, lb.getProtocol(), + Purpose.LoadBalancing, FirewallRuleType.User); + + Long networkId = ipAddr.getAssociatedWithNetworkId(); + if (networkId == null) { + List idList = new ArrayList(); + idList.add(new IdentityProxy(ipAddr, sourceIpId, "sourceIpid")); + throw new InvalidParameterValueException("Unable to create load balancer rule ; specified sourceip id is not associated with any network", idList); + + } + NetworkVO network = _networkDao.findById(networkId); + + _accountMgr.checkAccess(caller.getCaller(), null, true, ipAddr); + + // verify that lb service is supported by the network + if (!_networkMgr.areServicesSupportedInNetwork(network.getId(), Service.Lb)) { + List idList = new ArrayList(); + idList.add(new IdentityProxy(network, networkId, "networkId")); + throw new InvalidParameterValueException("LB service is not supported in network with specified id", idList); + } + + Transaction txn = Transaction.currentTxn(); + txn.start(); + + LoadBalancerVO newRule = new LoadBalancerVO(lb.getXid(), lb.getName(), lb.getDescription(), lb.getSourceIpAddressId(), lb.getSourcePortEnd(), lb.getDefaultPortStart(), + lb.getAlgorithm(), network.getId(), ipAddr.getAllocatedToAccountId(), ipAddr.getAllocatedInDomainId()); + + newRule = _lbDao.persist(newRule); + + if (openFirewall) { + _firewallMgr.createRuleForAllCidrs(sourceIpId, caller.getCaller(), lb.getSourcePortStart(), + lb.getSourcePortEnd(), lb.getProtocol(), null, null, newRule.getId(), networkId); + } + + boolean success = true; + + try { + _firewallMgr.detectRulesConflict(newRule); + if (!_firewallDao.setStateToAdd(newRule)) { + throw new CloudRuntimeException("Unable to update the state to add for " + newRule); + } + s_logger.debug("Load balancer " + newRule.getId() + " for Ip address id=" + sourceIpId + ", public port " + srcPortStart + ", private port " + defPortStart + " is added successfully."); + UserContext.current().setEventDetails("Load balancer Id: " + newRule.getId()); + UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_LOAD_BALANCER_CREATE, ipAddr.getAllocatedToAccountId(), ipAddr.getDataCenterId(), newRule.getId(), null); + _usageEventDao.persist(usageEvent); + txn.commit(); + + return newRule; + } catch (Exception e) { + success = false; + if (e instanceof NetworkRuleConflictException) { + throw (NetworkRuleConflictException) e; + } + throw new CloudRuntimeException("Unable to add rule for ip address id=" + newRule.getSourceIpAddressId(), e); + } finally { + if (!success && newRule != null) { + + txn.start(); + _firewallMgr.revokeRelatedFirewallRule(newRule.getId(), false); + removeLBRule(newRule); + + txn.commit(); + } + } + } + + @Override + public boolean applyLoadBalancerConfig(long lbRuleId) throws ResourceUnavailableException { + LoadBalancerVO lb = _lbDao.findById(lbRuleId); + List lbs; + if (isRollBackAllowedForProvider(lb)) { + // this is for Netscalar type of devices. if their is failure the db entries will be rollbacked. + lbs = Arrays.asList(lb); + } else { + // get all rules in transition state + lbs = _lbDao.listInTransitionStateByNetworkId(lb.getNetworkId()); + } + return applyLoadBalancerRules(lbs, true); + } + + @Override + public boolean applyLoadBalancersForNetwork(long networkId) throws ResourceUnavailableException { + List lbs = _lbDao.listByNetworkId(networkId); + if (lbs != null) { + return applyLoadBalancerRules(lbs, true); + } else { + s_logger.info("Network id=" + networkId + " doesn't have load balancer rules, nothing to apply"); + return true; + } + } + + @DB + protected boolean applyLoadBalancerRules(List lbs, boolean updateRulesInDB) throws ResourceUnavailableException { + Transaction txn = Transaction.currentTxn(); + List rules = new ArrayList(); + for (LoadBalancerVO lb : lbs) { + List dstList = getExistingDestinations(lb.getId()); + List policyList = getStickinessPolicies(lb.getId()); + + LoadBalancingRule loadBalancing = new LoadBalancingRule(lb, dstList, policyList); + rules.add(loadBalancing); + } + + if (!_networkMgr.applyRules(rules, false)) { + s_logger.debug("LB rules are not completely applied"); + return false; + } + + if (updateRulesInDB) { + for (LoadBalancerVO lb : lbs) { + boolean checkForReleaseElasticIp = false; + txn.start(); + if (lb.getState() == FirewallRule.State.Revoke) { + removeLBRule(lb); + s_logger.debug("LB " + lb.getId() + " is successfully removed"); + checkForReleaseElasticIp = true; + } else if (lb.getState() == FirewallRule.State.Add) { + lb.setState(FirewallRule.State.Active); + s_logger.debug("LB rule " + lb.getId() + " state is set to Active"); + _lbDao.persist(lb); + } + + // remove LB-Vm mappings that were state to revoke + List lbVmMaps = _lb2VmMapDao.listByLoadBalancerId(lb.getId(), true); + List instanceIds = new ArrayList(); + + for (LoadBalancerVMMapVO lbVmMap : lbVmMaps) { + instanceIds.add(lbVmMap.getInstanceId()); + } + + if (!instanceIds.isEmpty()) { + _lb2VmMapDao.remove(lb.getId(), instanceIds, null); + s_logger.debug("Load balancer rule id " + lb.getId() + " is removed for vms " + instanceIds); + } + + if (_lb2VmMapDao.listByLoadBalancerId(lb.getId()).isEmpty()) { + lb.setState(FirewallRule.State.Add); + _lbDao.persist(lb); + s_logger.debug("LB rule " + lb.getId() + " state is set to Add as there are no more active LB-VM mappings"); + } + + // remove LB-Stickiness policy mapping that were state to revoke + List stickinesspolicies = _lb2stickinesspoliciesDao.listByLoadBalancerId(lb.getId(), true); + if (!stickinesspolicies.isEmpty()) { + _lb2stickinesspoliciesDao.remove(lb.getId(), true); + s_logger.debug("Load balancer rule id " + lb.getId() + " is removed stickiness policies"); + } + + txn.commit(); + + if (checkForReleaseElasticIp) { + boolean success = true; + long count = _firewallDao.countRulesByIpId(lb.getSourceIpAddressId()); + if (count == 0) { + try { + success = handleSystemLBIpRelease(lb); + } catch (Exception ex) { + s_logger.warn("Failed to release system ip as a part of lb rule " + lb + " deletion due to exception ", ex); + success = false; + } finally { + if (!success) { + s_logger.warn("Failed to release system ip as a part of lb rule " + lb + " deletion"); + } + } + } + } + } + } + + return true; + } + + protected boolean handleSystemLBIpRelease(LoadBalancerVO lb) { + IpAddress ip = _ipAddressDao.findById(lb.getSourceIpAddressId()); + boolean success = true; + if (ip.getSystem()) { + s_logger.debug("Releasing system ip address " + lb.getSourceIpAddressId() + " as a part of delete lb rule"); + if (!_networkMgr.disassociatePublicIpAddress(lb.getSourceIpAddressId(), UserContext.current().getCallerUserId(), UserContext.current().getCaller())) { + s_logger.warn("Unable to release system ip address id=" + lb.getSourceIpAddressId() + " as a part of delete lb rule"); + success = false; + } else { + s_logger.warn("Successfully released system ip address id=" + lb.getSourceIpAddressId() + " as a part of delete lb rule"); + } + } + + return success; + } + + @Override + public boolean removeAllLoadBalanacersForIp(long ipId, Account caller, long callerUserId) { + List rules = _firewallDao.listByIpAndPurposeAndNotRevoked(ipId, Purpose.LoadBalancing); + if (rules != null) { + s_logger.debug("Found " + rules.size() + " lb rules to cleanup"); + } + for (FirewallRule rule : rules) { + boolean result = deleteLoadBalancerRule(rule.getId(), true, caller, callerUserId, false); + if (result == false) { + s_logger.warn("Unable to remove load balancer rule " + rule.getId()); + return false; + } + } + return true; + } + + @Override + public boolean removeAllLoadBalanacersForNetwork(long networkId, Account caller, long callerUserId) { + List rules = _firewallDao.listByNetworkAndPurposeAndNotRevoked(networkId, Purpose.LoadBalancing); + if (rules != null) { + s_logger.debug("Found " + rules.size() + " lb rules to cleanup"); + } + for (FirewallRule rule : rules) { + boolean result = deleteLoadBalancerRule(rule.getId(), true, caller, callerUserId, false); + if (result == false) { + s_logger.warn("Unable to remove load balancer rule " + rule.getId()); + return false; + } + } + return true; + } + + @Override + public List getStickinessPolicies(long lbId) { + List stickinessPolicies = new ArrayList(); + List sDbpolicies = _lb2stickinesspoliciesDao.listByLoadBalancerId(lbId); + + for (LBStickinessPolicyVO sDbPolicy : sDbpolicies) { + LbStickinessPolicy sPolicy = new LbStickinessPolicy(sDbPolicy.getMethodName(), sDbPolicy.getParams(), sDbPolicy.isRevoke()); + stickinessPolicies.add(sPolicy); + } + return stickinessPolicies; + } + + @Override + public List getExistingDestinations(long lbId) { + List dstList = new ArrayList(); + List lbVmMaps = _lb2VmMapDao.listByLoadBalancerId(lbId); + LoadBalancerVO lb = _lbDao.findById(lbId); + + String dstIp = null; + for (LoadBalancerVMMapVO lbVmMap : lbVmMaps) { + UserVm vm = _vmDao.findById(lbVmMap.getInstanceId()); + Nic nic = _nicDao.findByInstanceIdAndNetworkIdIncludingRemoved(lb.getNetworkId(), vm.getId()); + dstIp = nic.getIp4Address(); + LbDestination lbDst = new LbDestination(lb.getDefaultPortStart(), lb.getDefaultPortEnd(), dstIp, lbVmMap.isRevoke()); + dstList.add(lbDst); + } + return dstList; + } + + @Override + public boolean configure(String name, Map params) throws ConfigurationException { + _name = name; + return true; + } + + @Override + public boolean start() { + return true; + } + + @Override + public boolean stop() { + return true; + } + + @Override + public String getName() { + return _name; + } + + @Override + @ActionEvent(eventType = EventTypes.EVENT_LOAD_BALANCER_UPDATE, eventDescription = "updating load balancer", async = true) + public LoadBalancer updateLoadBalancerRule(UpdateLoadBalancerRuleCmd cmd) { + Account caller = UserContext.current().getCaller(); + Long lbRuleId = cmd.getId(); + String name = cmd.getLoadBalancerName(); + String description = cmd.getDescription(); + String algorithm = cmd.getAlgorithm(); + LoadBalancerVO lb = _lbDao.findById(lbRuleId); + LoadBalancerVO lbBackup = _lbDao.findById(lbRuleId); + + if (lb == null) { + throw new InvalidParameterValueException("Unable to find lb rule by id", null); + } + + // check permissions + _accountMgr.checkAccess(caller, null, true, lb); + + if (name != null) { + lb.setName(name); + } + + if (description != null) { + lb.setDescription(description); + } + + if (algorithm != null) { + lb.setAlgorithm(algorithm); + } + + boolean success = _lbDao.update(lbRuleId, lb); + + // If algorithm is changed, have to reapply the lb config + if (algorithm != null) { + try { + lb.setState(FirewallRule.State.Add); + _lbDao.persist(lb); + applyLoadBalancerConfig(lbRuleId); + } catch (ResourceUnavailableException e) { + if (isRollBackAllowedForProvider(lb)) { + /* + * NOTE : We use lb object to update db instead of lbBackup object since db layer will fail to + * update if there is no change in the object. + */ + if (lbBackup.getName() != null) { + lb.setName(lbBackup.getName()); + } + if (lbBackup.getDescription() != null) { + lb.setDescription(lbBackup.getDescription()); + } + if (lbBackup.getAlgorithm() != null) { + lb.setAlgorithm(lbBackup.getAlgorithm()); + } + lb.setState(lbBackup.getState()); + _lbDao.update(lb.getId(), lb); + _lbDao.persist(lb); + + s_logger.debug("LB Rollback rule id: " + lbRuleId + " while updating LB rule."); + } + s_logger.warn("Unable to apply the load balancer config because resource is unavaliable.", e); + success = false; + } + } + + if (!success) { + throw new CloudRuntimeException("Failed to update load balancer rule: " + lbRuleId); + } + + return lb; + } + + @Override + public List listLoadBalancerInstances(ListLoadBalancerRuleInstancesCmd cmd) throws PermissionDeniedException { + Account caller = UserContext.current().getCaller(); + Long loadBalancerId = cmd.getId(); + Boolean applied = cmd.isApplied(); + + if (applied == null) { + applied = Boolean.TRUE; + } + + LoadBalancerVO loadBalancer = _lbDao.findById(loadBalancerId); + if (loadBalancer == null) { + return null; + } + + _accountMgr.checkAccess(caller, null, true, loadBalancer); + + List loadBalancerInstances = new ArrayList(); + List vmLoadBalancerMappings = null; + + vmLoadBalancerMappings = _lb2VmMapDao.listByLoadBalancerId(loadBalancerId); + + List appliedInstanceIdList = new ArrayList(); + if ((vmLoadBalancerMappings != null) && !vmLoadBalancerMappings.isEmpty()) { + for (LoadBalancerVMMapVO vmLoadBalancerMapping : vmLoadBalancerMappings) { + appliedInstanceIdList.add(vmLoadBalancerMapping.getInstanceId()); + } + } + + IPAddressVO addr = _ipAddressDao.findById(loadBalancer.getSourceIpAddressId()); + List userVms = _vmDao.listVirtualNetworkInstancesByAcctAndZone(loadBalancer.getAccountId(), addr.getDataCenterId(), loadBalancer.getNetworkId()); + + for (UserVmVO userVm : userVms) { + // if the VM is destroyed, being expunged, in an error state, or in an unknown state, skip it + switch (userVm.getState()) { + case Destroyed: + case Expunging: + case Error: + case Unknown: + continue; + } + + boolean isApplied = appliedInstanceIdList.contains(userVm.getId()); + if ((isApplied && applied) || (!isApplied && !applied)) { + loadBalancerInstances.add(userVm); + } + } + + return loadBalancerInstances; + } + + public List getSupportedAutoScaleCounters(long networkid) + { + String capability = getLBCapability(networkid, Capability.AutoScaleCounters.getName()); + if (capability == null || capability.length() == 0) { + return null; + } + return Arrays.asList(capability.split(",")); + } + + @Override + public List getStickinessMethods(long networkid) + { + String capability = getLBCapability(networkid, Capability.SupportedStickinessMethods.getName()); + if (capability == null) { + return null; + } + Gson gson = new Gson(); + java.lang.reflect.Type listType = new TypeToken>() { + }.getType(); + List result = gson.fromJson(capability, listType); + return result; + } + + @Override + public List searchForLBStickinessPolicies(ListLBStickinessPoliciesCmd cmd) throws PermissionDeniedException { + Account caller = UserContext.current().getCaller(); + Long loadBalancerId = cmd.getLbRuleId(); + LoadBalancerVO loadBalancer = _lbDao.findById(loadBalancerId); + if (loadBalancer == null) { + return null; + } + + _accountMgr.checkAccess(caller, null, true, loadBalancer); + + List sDbpolicies = _lb2stickinesspoliciesDao.listByLoadBalancerId(cmd.getLbRuleId()); + + return sDbpolicies; + } + + @Override + public List searchForLoadBalancers(ListLoadBalancerRulesCmd cmd) { + Long ipId = cmd.getPublicIpId(); + Long zoneId = cmd.getZoneId(); + Long id = cmd.getId(); + String name = cmd.getLoadBalancerRuleName(); + String keyword = cmd.getKeyword(); + Long instanceId = cmd.getVirtualMachineId(); + Map tags = cmd.getTags(); + + Account caller = UserContext.current().getCaller(); + List permittedAccounts = new ArrayList(); + + Ternary domainIdRecursiveListProject = new Ternary(cmd.getDomainId(), cmd.isRecursive(), null); + _accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, cmd.listAll(), false); + Long domainId = domainIdRecursiveListProject.first(); + Boolean isRecursive = domainIdRecursiveListProject.second(); + ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third(); + + Filter searchFilter = new Filter(LoadBalancerVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal()); + SearchBuilder sb = _lbDao.createSearchBuilder(); + _accountMgr.buildACLSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria); + + sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ); + sb.and("name", sb.entity().getName(), SearchCriteria.Op.LIKE); + sb.and("sourceIpAddress", sb.entity().getSourceIpAddressId(), SearchCriteria.Op.EQ); + + if (instanceId != null) { + SearchBuilder lbVMSearch = _lb2VmMapDao.createSearchBuilder(); + lbVMSearch.and("instanceId", lbVMSearch.entity().getInstanceId(), SearchCriteria.Op.EQ); + sb.join("lbVMSearch", lbVMSearch, sb.entity().getId(), lbVMSearch.entity().getLoadBalancerId(), JoinBuilder.JoinType.INNER); + } + + if (zoneId != null) { + SearchBuilder ipSearch = _ipAddressDao.createSearchBuilder(); + ipSearch.and("zoneId", ipSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ); + sb.join("ipSearch", ipSearch, sb.entity().getSourceIpAddressId(), ipSearch.entity().getId(), JoinBuilder.JoinType.INNER); + } + + if (tags != null && !tags.isEmpty()) { + SearchBuilder tagSearch = _resourceTagDao.createSearchBuilder(); + for (int count = 0; count < tags.size(); count++) { + tagSearch.or().op("key" + String.valueOf(count), tagSearch.entity().getKey(), SearchCriteria.Op.EQ); + tagSearch.and("value" + String.valueOf(count), tagSearch.entity().getValue(), SearchCriteria.Op.EQ); + tagSearch.cp(); + } + tagSearch.and("resourceType", tagSearch.entity().getResourceType(), SearchCriteria.Op.EQ); + sb.groupBy(sb.entity().getId()); + sb.join("tagSearch", tagSearch, sb.entity().getId(), tagSearch.entity().getResourceId(), JoinBuilder.JoinType.INNER); + } + + SearchCriteria sc = sb.create(); + _accountMgr.buildACLSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria); + + if (keyword != null) { + SearchCriteria ssc = _lbDao.createSearchCriteria(); + ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%"); + ssc.addOr("description", SearchCriteria.Op.LIKE, "%" + keyword + "%"); + sc.addAnd("name", SearchCriteria.Op.SC, ssc); + } + + if (name != null) { + sc.setParameters("name", "%" + name + "%"); + } + + if (id != null) { + sc.setParameters("id", id); + } + + if (ipId != null) { + sc.setParameters("sourceIpAddress", ipId); + } + + if (instanceId != null) { + sc.setJoinParameters("lbVMSearch", "instanceId", instanceId); + } + + if (zoneId != null) { + sc.setJoinParameters("ipSearch", "zoneId", zoneId); + } + + if (tags != null && !tags.isEmpty()) { + int count = 0; + sc.setJoinParameters("tagSearch", "resourceType", TaggedResourceType.LoadBalancer.toString()); + for (String key : tags.keySet()) { + sc.setJoinParameters("tagSearch", "key" + String.valueOf(count), key); + sc.setJoinParameters("tagSearch", "value" + String.valueOf(count), tags.get(key)); + count++; + } + } + + return _lbDao.search(sc, searchFilter); + } + + @Override + public List listByNetworkId(long networkId) { + List lbs = _lbDao.listByNetworkId(networkId); + List lbRules = new ArrayList(); + for (LoadBalancerVO lb : lbs) { + List dstList = getExistingDestinations(lb.getId()); + List policyList = this.getStickinessPolicies(lb.getId()); + LoadBalancingRule loadBalancing = new LoadBalancingRule(lb, dstList, policyList); + lbRules.add(loadBalancing); + } + return lbRules; + } + + @Override + public LoadBalancerVO findById(long lbId) { + return _lbDao.findById(lbId); + } + + protected void removeLBRule(LoadBalancerVO rule) { + + //remove the rule + _lbDao.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()); + _vpcMgr.unassignIPFromVpcNetwork(ip.getId(), rule.getNetworkId()); + + } +} diff --git a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java index ba25d7256f1..6f40d165d6a 100755 --- a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java +++ b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java @@ -132,6 +132,7 @@ import com.cloud.network.RemoteAccessVpn; import com.cloud.network.Site2SiteCustomerGateway; 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; @@ -763,15 +764,15 @@ VirtualMachineGuru, Listener { String privateIP = router.getPrivateIpAddress(); if (privateIP != null) { + boolean forVpc = router.getVpcId() != null; List routerNics = _nicDao.listByVmId(router.getId()); for (Nic routerNic : routerNics) { Network network = _networkMgr.getNetwork(routerNic.getNetworkId()); - if (network.getTrafficType() == TrafficType.Public) { - boolean forVpc = router.getVpcId() != null; + if ((forVpc && network.getTrafficType() == TrafficType.Public) || (!forVpc && network.getTrafficType() == TrafficType.Guest)) { final NetworkUsageCommand usageCmd = new NetworkUsageCommand(privateIP, router.getHostName(), forVpc, routerNic.getIp4Address()); UserStatisticsVO previousStats = _statsDao.findBy(router.getAccountId(), - router.getDataCenterIdToDeployIn(), network.getId(), null, router.getId(), router.getType().toString()); + router.getDataCenterIdToDeployIn(), network.getId(), (forVpc ? routerNic.getIp4Address() : null), router.getId(), router.getType().toString()); NetworkUsageAnswer answer = null; try { answer = (NetworkUsageAnswer) _agentMgr.easySend(router.getHostId(), usageCmd); @@ -793,7 +794,7 @@ VirtualMachineGuru, Listener { } txn.start(); UserStatisticsVO stats = _statsDao.lock(router.getAccountId(), - router.getDataCenterIdToDeployIn(), network.getId(), routerNic.getIp4Address(), router.getId(), router.getType().toString()); + router.getDataCenterIdToDeployIn(), network.getId(), (forVpc ? routerNic.getIp4Address() : null), router.getId(), router.getType().toString()); if (stats == null) { s_logger.warn("unable to find stats for account: " + router.getAccountId()); continue; @@ -838,6 +839,81 @@ VirtualMachineGuru, Listener { txn.close(); } } + if(forVpc){ + //Get VPN gateway + Site2SiteVpnGatewayVO s2sVpn = _s2sVpnGatewayDao.findByVpcId(router.getVpcId()); + if(s2sVpn != null){ + final NetworkUsageCommand vpnUsageCmd = new NetworkUsageCommand(privateIP, router.getHostName(), "vpn", forVpc, routerNic.getIp4Address()); + previousStats = _statsDao.findBy(s2sVpn.getAccountId(), router.getDataCenterIdToDeployIn(), network.getId(), + routerNic.getIp4Address(), s2sVpn.getId(), "VPNGateway"); + answer = null; + try { + answer = (NetworkUsageAnswer) _agentMgr.easySend(router.getHostId(), vpnUsageCmd); + } catch (Exception e) { + s_logger.warn("Error while collecting vpn network stats from router: "+router.getInstanceName()+" from host: "+router.getHostId(), e); + continue; + } + + if (answer != null) { + if (!answer.getResult()) { + s_logger.warn("Error while collecting vpn network stats from router: "+router.getInstanceName()+" from host: "+router.getHostId() + "; details: " + answer.getDetails()); + continue; + } + Transaction txn = Transaction.open(Transaction.CLOUD_DB); + try { + if ((answer.getBytesReceived() == 0) && (answer.getBytesSent() == 0)) { + s_logger.debug("Recieved and Sent bytes are both 0. Not updating user_statistics"); + continue; + } + txn.start(); + UserStatisticsVO stats = _statsDao.lock(s2sVpn.getAccountId(), router.getDataCenterIdToDeployIn(), network.getId(), + routerNic.getIp4Address(), s2sVpn.getId(), "VPNGateway"); + if (stats == null) { + s_logger.warn("unable to find vpn stats for account: " + router.getAccountId()+" vpc Id: "+router.getVpcId()); + 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()); + 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() + + " Stored: " + stats.getCurrentBytesReceived()); + } + stats.setNetBytesReceived(stats.getNetBytesReceived() + stats.getCurrentBytesReceived()); + } + stats.setCurrentBytesReceived(answer.getBytesReceived()); + if (stats.getCurrentBytesSent() > answer.getBytesSent()) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Received # of bytes that's less than the last one. " + + "Assuming something went wrong and persisting it. Router: " + + answer.getRouterName()+" Reported: " + answer.getBytesSent() + + " Stored: " + stats.getCurrentBytesSent()); + } + stats.setNetBytesSent(stats.getNetBytesSent() + stats.getCurrentBytesSent()); + } + stats.setCurrentBytesSent(answer.getBytesSent()); + _statsDao.update(stats.getId(), stats); + 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()); + } finally { + txn.close(); + } + } + } + } } } } @@ -900,6 +976,7 @@ VirtualMachineGuru, Listener { } } + @DB protected void updateSite2SiteVpnConnectionState(List routers) { for (DomainRouterVO router : routers) { List conns = _s2sVpnMgr.getConnectionsForRouter(router); @@ -949,26 +1026,34 @@ VirtualMachineGuru, Listener { continue; } for (Site2SiteVpnConnectionVO conn : conns) { - if (conn.getState() != Site2SiteVpnConnection.State.Connected && - conn.getState() != Site2SiteVpnConnection.State.Disconnected) { - continue; + Site2SiteVpnConnectionVO lock = _s2sVpnConnectionDao.acquireInLockTable(conn.getId()); + if (lock == null) { + throw new CloudRuntimeException("Unable to acquire lock on " + lock); } - Site2SiteVpnConnection.State oldState = conn.getState(); - Site2SiteCustomerGateway gw = _s2sCustomerGatewayDao.findById(conn.getCustomerGatewayId()); - if (answer.isConnected(gw.getGatewayIp())) { - conn.setState(Site2SiteVpnConnection.State.Connected); - } else { - conn.setState(Site2SiteVpnConnection.State.Disconnected); - } - _s2sVpnConnectionDao.persist(conn); - if (oldState != conn.getState()) { - String title = "Site-to-site Vpn Connection to " + gw.getName() + - " just switch from " + oldState + " to " + conn.getState(); - String context = "Site-to-site Vpn Connection to " + gw.getName() + " on router " + router.getHostName() + - "(id: " + router.getId() + ") " + " just switch from " + oldState + " to " + conn.getState(); - s_logger.info(context); - _alertMgr.sendAlert(AlertManager.ALERT_TYPE_DOMAIN_ROUTER, - router.getDataCenterIdToDeployIn(), router.getPodIdToDeployIn(), title, context); + try { + if (conn.getState() != Site2SiteVpnConnection.State.Connected && + conn.getState() != Site2SiteVpnConnection.State.Disconnected) { + continue; + } + Site2SiteVpnConnection.State oldState = conn.getState(); + Site2SiteCustomerGateway gw = _s2sCustomerGatewayDao.findById(conn.getCustomerGatewayId()); + if (answer.isConnected(gw.getGatewayIp())) { + conn.setState(Site2SiteVpnConnection.State.Connected); + } else { + conn.setState(Site2SiteVpnConnection.State.Disconnected); + } + _s2sVpnConnectionDao.persist(conn); + if (oldState != conn.getState()) { + String title = "Site-to-site Vpn Connection to " + gw.getName() + + " just switch from " + oldState + " to " + conn.getState(); + String context = "Site-to-site Vpn Connection to " + gw.getName() + " on router " + router.getHostName() + + "(id: " + router.getId() + ") " + " just switch from " + oldState + " to " + conn.getState(); + s_logger.info(context); + _alertMgr.sendAlert(AlertManager.ALERT_TYPE_DOMAIN_ROUTER, + router.getDataCenterIdToDeployIn(), router.getPodIdToDeployIn(), title, context); + } + } finally { + _s2sVpnConnectionDao.releaseFromLockTable(lock.getId()); } } } @@ -2311,6 +2396,11 @@ VirtualMachineGuru, Listener { ConcurrentOperationException, ResourceUnavailableException { s_logger.debug("Starting router " + router); if (_itMgr.start(router, params, user, caller, planToDeploy) != null) { + // We don't want the failure of VPN Connection affect the status of router, so we try to make connection only after router start successfully + Long vpcId = router.getVpcId(); + if (vpcId != null) { + _s2sVpnMgr.reconnectDisconnectedVpnByVpc(vpcId); + } return _routerDao.findById(router.getId()); } else { return null; @@ -2375,9 +2465,19 @@ VirtualMachineGuru, Listener { if (offering.getRedundantRouter()) { return findGatewayIp(userVmId); } + + DataCenter dc = _dcDao.findById(_networkMgr.getNetwork(defaultNic.getNetworkId()).getDataCenterId()); + boolean isZoneBasic = (dc.getNetworkType() == NetworkType.Basic); //find domR's nic in the network - NicVO domrDefaultNic = _nicDao.findByNetworkIdAndType(defaultNic.getNetworkId(), VirtualMachine.Type.DomainRouter); + NicVO domrDefaultNic; + if (isZoneBasic){ + domrDefaultNic = _nicDao.findByNetworkIdTypeAndGateway(defaultNic.getNetworkId(), VirtualMachine.Type.DomainRouter, defaultNic.getGateway()); + } + else{ + domrDefaultNic = _nicDao.findByNetworkIdAndType(defaultNic.getNetworkId(), VirtualMachine.Type.DomainRouter); + } + return domrDefaultNic.getIp4Address(); } @@ -3282,12 +3382,12 @@ VirtualMachineGuru, Listener { List routerNics = _nicDao.listByVmId(router.getId()); for (Nic routerNic : routerNics) { Network network = _networkMgr.getNetwork(routerNic.getNetworkId()); - if (network.getTrafficType() == TrafficType.Public) { - boolean forVpc = router.getVpcId() != null; + boolean forVpc = router.getVpcId() != null; + if ((forVpc && network.getTrafficType() == TrafficType.Public) || (!forVpc && network.getTrafficType() == TrafficType.Guest)) { final NetworkUsageCommand usageCmd = new NetworkUsageCommand(privateIP, router.getHostName(), forVpc, routerNic.getIp4Address()); UserStatisticsVO previousStats = _statsDao.findBy(router.getAccountId(), - router.getDataCenterIdToDeployIn(), network.getId(), null, router.getId(), router.getType().toString()); + router.getDataCenterIdToDeployIn(), network.getId(), (forVpc ? routerNic.getIp4Address() : null), router.getId(), router.getType().toString()); NetworkUsageAnswer answer = null; try { answer = (NetworkUsageAnswer) _agentMgr.easySend(router.getHostId(), usageCmd); @@ -3309,7 +3409,7 @@ VirtualMachineGuru, Listener { } txn.start(); UserStatisticsVO stats = _statsDao.lock(router.getAccountId(), - router.getDataCenterIdToDeployIn(), network.getId(), null, router.getId(), router.getType().toString()); + router.getDataCenterIdToDeployIn(), network.getId(), (forVpc ? routerNic.getIp4Address() : null), router.getId(), router.getType().toString()); if (stats == null) { s_logger.warn("unable to find stats for account: " + router.getAccountId()); continue; diff --git a/server/src/com/cloud/network/router/VpcVirtualNetworkApplianceManagerImpl.java b/server/src/com/cloud/network/router/VpcVirtualNetworkApplianceManagerImpl.java index 5e0328bb671..619575cccb2 100644 --- a/server/src/com/cloud/network/router/VpcVirtualNetworkApplianceManagerImpl.java +++ b/server/src/com/cloud/network/router/VpcVirtualNetworkApplianceManagerImpl.java @@ -113,6 +113,7 @@ import com.cloud.utils.net.NetUtils; import com.cloud.vm.DomainRouterVO; import com.cloud.vm.Nic; import com.cloud.vm.NicProfile; +import com.cloud.vm.NicVO; import com.cloud.vm.ReservationContext; import com.cloud.vm.VirtualMachine; import com.cloud.vm.VirtualMachine.State; @@ -128,6 +129,7 @@ import com.cloud.vm.dao.VMInstanceDao; public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplianceManagerImpl implements VpcVirtualNetworkApplianceManager{ private static final Logger s_logger = Logger.getLogger(VpcVirtualNetworkApplianceManagerImpl.class); + String _name; @Inject VpcDao _vpcDao; @Inject @@ -333,15 +335,13 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian PlugNicCommand plugNicCmd = new PlugNicCommand(nic, vm.getName()); Commands cmds = new Commands(OnError.Stop); - cmds.addCommand("plugnic", plugNicCmd); + cmds.addCommand("plugnic", plugNicCmd); _agentMgr.send(dest.getHost().getId(), cmds); - PlugNicAnswer plugNicAnswer = cmds.getAnswer(PlugNicAnswer.class); if (!(plugNicAnswer != null && plugNicAnswer.getResult())) { s_logger.warn("Unable to plug nic for vm " + vm.getHostName()); result = false; } - } catch (OperationTimedoutException e) { throw new AgentUnavailableException("Unable to plug nic for router " + vm.getHostName() + " in network " + network, dest.getHost().getId(), e); @@ -365,8 +365,12 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian if (router.getState() == State.Running) { try { + Commands cmds = new Commands(OnError.Stop); + if(network.getTrafficType() == TrafficType.Public){ + NetworkUsageCommand netUsageCmd = new NetworkUsageCommand(router.getPrivateIpAddress(), router.getInstanceName(), "remove", true, nic.getIp()); + cmds.addCommand(netUsageCmd); + } UnPlugNicCommand unplugNicCmd = new UnPlugNicCommand(nic, vm.getName()); - Commands cmds = new Commands(OnError.Stop); cmds.addCommand("unplugnic", unplugNicCmd); _agentMgr.send(dest.getHost().getId(), cmds); @@ -374,8 +378,14 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian if (!(unplugNicAnswer != null && unplugNicAnswer.getResult())) { s_logger.warn("Unable to unplug nic from router " + router); result = false; - } - + } else { + if(network.getTrafficType() == TrafficType.Public){ + NetworkUsageCommand netUsageCmd = new NetworkUsageCommand(router.getPrivateIpAddress(), router.getInstanceName(), "remove", true, nic.getIp()); + cmds = new Commands(OnError.Stop); + cmds.addCommand(netUsageCmd); + _agentMgr.send(dest.getHost().getId(), cmds); + } + } } catch (OperationTimedoutException e) { throw new AgentUnavailableException("Unable to unplug nic from rotuer " + router + " from network " + network, dest.getHost().getId(), e); @@ -568,7 +578,10 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian return false; } } - + + Commands netUsagecmds = new Commands(OnError.Continue); + VpcVO vpc = _vpcDao.findById(router.getVpcId()); + //2) Plug the nics for (String vlanTag : nicsToPlug.keySet()) { PublicIpAddress ip = nicsToPlug.get(vlanTag); @@ -603,6 +616,16 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian return false; } } + //Create network usage commands. Send commands to router after IPAssoc + NetworkUsageCommand netUsageCmd = new NetworkUsageCommand(router.getPrivateIpAddress(), router.getInstanceName(), true, defaultNic.getIp4Address(), vpc.getCidr()); + netUsagecmds.addCommand(netUsageCmd); + UserStatisticsVO stats = _userStatsDao.findBy(router.getAccountId(), router.getDataCenterIdToDeployIn(), + publicNtwk.getId(), publicNic.getIp4Address(), router.getId(), router.getType().toString()); + if (stats == null) { + stats = new UserStatisticsVO(router.getAccountId(), router.getDataCenterIdToDeployIn(), publicNic.getIp4Address(), router.getId(), + router.getType().toString(), publicNtwk.getId()); + _userStatsDao.persist(stats); + } } //3) apply the rules @@ -634,7 +657,10 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian return sendCommandsToRouter(router, cmds); } }); - + if(result && netUsagecmds.size() > 0){ + //After successful ipassoc, send commands to router + sendCommandsToRouter(router, netUsagecmds); + } return result; } @@ -1118,7 +1144,7 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian String localGuestCidr = vpc.getCidr(); String localPublicGateway = _vlanDao.findById(ip.getVlanId()).getVlanGateway(); String peerGatewayIp = gw.getGatewayIp(); - String peerGuestCidrList = gw.getGuestCidrList(); + String peerGuestCidrList = gw.getGuestCidrList().replace(";", ","); String ipsecPsk = gw.getIpsecPsk(); String ikePolicy = gw.getIkePolicy(); String espPolicy = gw.getEspPolicy(); @@ -1325,10 +1351,19 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian Nic nic = _nicDao.findByNetworkIdInstanceIdAndBroadcastUri(publicNtwkId, router.getId(), broadcastUri.toString()); - if ((nic == null && nicsToPlug.get(ip.getVlanTag()) == null) || nicsToUnplug.get(ip.getVlanTag()) != null) { + if (nic == null && nicsToPlug.get(ip.getVlanTag()) == null) { nicsToPlug.put(ip.getVlanTag(), ip); s_logger.debug("Need to plug the nic for ip=" + ip + "; vlan=" + ip.getVlanTag() + " in public network id =" + publicNtwkId); + } else { + PublicIpAddress nicToUnplug = nicsToUnplug.get(ip.getVlanTag()); + if (nicToUnplug != null) { + NicVO nicVO = _nicDao.findByIp4AddressAndNetworkIdAndInstanceId(publicNtwkId, router.getId(), nicToUnplug.getAddress().addr()); + nicVO.setIp4Address(ip.getAddress().addr()); + _nicDao.update(nicVO.getId(), nicVO); + s_logger.debug("Updated the nic " + nicVO + " with the new ip address " + ip.getAddress().addr()); + nicsToUnplug.remove(ip.getVlanTag()); + } } } } @@ -1348,4 +1383,5 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian _s2sVpnMgr.markDisconnectVpnConnByVpc(vpcId); } } + } diff --git a/server/src/com/cloud/network/vpc/VpcManagerImpl.java b/server/src/com/cloud/network/vpc/VpcManagerImpl.java index 7cb7796ef1d..f1ca879683c 100644 --- a/server/src/com/cloud/network/vpc/VpcManagerImpl.java +++ b/server/src/com/cloud/network/vpc/VpcManagerImpl.java @@ -207,8 +207,8 @@ public class VpcManagerImpl implements VpcManager, Manager{ String maxNtwks = configs.get(Config.VpcMaxNetworks.key()); _maxNetworks = NumbersUtil.parseInt(maxNtwks, 3); // max=3 is default - - + + IpAddressSearch = _ipAddressDao.createSearchBuilder(); IpAddressSearch.and("accountId", IpAddressSearch.entity().getAllocatedToAccountId(), Op.EQ); IpAddressSearch.and("dataCenterId", IpAddressSearch.entity().getDataCenterId(), Op.EQ); @@ -218,7 +218,7 @@ public class VpcManagerImpl implements VpcManager, Manager{ virtualNetworkVlanSB.and("vlanType", virtualNetworkVlanSB.entity().getVlanType(), Op.EQ); IpAddressSearch.join("virtualNetworkVlanSB", virtualNetworkVlanSB, IpAddressSearch.entity().getVlanId(), virtualNetworkVlanSB.entity().getId(), JoinBuilder.JoinType.INNER); IpAddressSearch.done(); - + return true; } @@ -517,7 +517,7 @@ public class VpcManagerImpl implements VpcManager, Manager{ //Verify that caller can perform actions in behalf of vpc owner _accountMgr.checkAccess(caller, null, false, owner); - + //check resource limit _resourceLimitMgr.checkResourceLimit(owner, ResourceType.vpc); @@ -633,7 +633,7 @@ public class VpcManagerImpl implements VpcManager, Manager{ @DB public boolean destroyVpc(Vpc vpc) throws ConcurrentOperationException, ResourceUnavailableException { UserContext ctx = UserContext.current(); - + s_logger.debug("Destroying vpc " + vpc); //don't allow to delete vpc if it's in use by existing networks int networksCount = _ntwkDao.getNetworkCountByVpcId(vpc.getId()); if (networksCount > 0) { @@ -647,16 +647,16 @@ public class VpcManagerImpl implements VpcManager, Manager{ s_logger.debug("Updating VPC " + vpc + " with state " + Vpc.State.Inactive + " as a part of vpc delete"); VpcVO vpcVO = _vpcDao.findById(vpc.getId()); vpcVO.setState(Vpc.State.Inactive); - + Transaction txn = Transaction.currentTxn(); txn.start(); _vpcDao.update(vpc.getId(), vpcVO); - + //decrement resource count _resourceLimitMgr.decrementResourceCount(vpc.getAccountId(), ResourceType.vpc); txn.commit(); } - + //shutdown VPC if (!shutdownVpc(vpc.getId())) { s_logger.warn("Failed to shutdown vpc " + vpc + " as a part of vpc destroy process"); @@ -671,9 +671,10 @@ public class VpcManagerImpl implements VpcManager, Manager{ //update the instance with removed flag only when the cleanup is executed successfully if (_vpcDao.remove(vpc.getId())) { - s_logger.debug("Vpc " + vpc + " is removed succesfully"); + s_logger.debug("Vpc " + vpc + " is destroyed succesfully"); return true; } else { + s_logger.warn("Vpc " + vpc + " failed to destroy"); return false; } } @@ -714,13 +715,13 @@ public class VpcManagerImpl implements VpcManager, Manager{ @Override public List listVpcs(Long id, String vpcName, String displayText, List supportedServicesStr, String cidr, Long vpcOffId, String state, String accountName, Long domainId, String keyword, - Long startIndex, Long pageSizeVal, Long zoneId, Boolean isRecursive, Boolean listAll, Boolean restartRequired, Map tags) { + Long startIndex, Long pageSizeVal, Long zoneId, Boolean isRecursive, Boolean listAll, Boolean restartRequired, Map tags, Long projectId) { Account caller = UserContext.current().getCaller(); List permittedAccounts = new ArrayList(); Ternary domainIdRecursiveListProject = new Ternary(domainId, isRecursive, null); - _accountMgr.buildACLSearchParameters(caller, id, accountName, null, permittedAccounts, domainIdRecursiveListProject, + _accountMgr.buildACLSearchParameters(caller, id, accountName, projectId, permittedAccounts, domainIdRecursiveListProject, listAll, false); domainId = domainIdRecursiveListProject.first(); isRecursive = domainIdRecursiveListProject.second(); @@ -839,7 +840,7 @@ public class VpcManagerImpl implements VpcManager, Manager{ } } - + protected List getSupportedServices() { List services = new ArrayList(); services.add(Network.Service.Dhcp); @@ -865,9 +866,7 @@ public class VpcManagerImpl implements VpcManager, Manager{ //check if vpc exists Vpc vpc = getActiveVpc(vpcId); if (vpc == null) { - InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find Enabled VPC by id specified", null); - ex.addProxyObject("vpc", vpcId, "VPC"); - throw ex; + throw new InvalidParameterValueException("Unable to find Enabled VPC", null); } //permission check @@ -929,9 +928,10 @@ public class VpcManagerImpl implements VpcManager, Manager{ _accountMgr.checkAccess(caller, null, false, vpc); //shutdown provider + s_logger.debug("Shutting down vpc " + vpc); boolean success = getVpcElement().shutdownVpc(vpc); - //TODO - cleanup all vpc resources here (ACLs, gateways, etc) + //TODO - shutdown all vpc resources here (ACLs, gateways, etc) if (success) { s_logger.debug("Vpc " + vpc + " has been shutdown succesfully"); } else { @@ -944,8 +944,12 @@ public class VpcManagerImpl implements VpcManager, Manager{ @DB public void validateNtkwOffForVpc(long ntwkOffId, String cidr, String networkDomain, Account networkOwner, Vpc vpc, Long networkId, String gateway) { - + NetworkOffering guestNtwkOff = _configMgr.getNetworkOffering(ntwkOffId); + + if (guestNtwkOff == null) { + throw new InvalidParameterValueException("Can't find network offering by id specified", null); + } if (networkId == null) { //1) Validate attributes that has to be passed in when create new guest network @@ -958,7 +962,7 @@ public class VpcManagerImpl implements VpcManager, Manager{ throw new InvalidParameterValueException("Only networks of type " + GuestType.Isolated + " with service " + Service.SourceNat.getName() + - " can be added as a part of VPC", null); + " can be added as a part of VPC ", null); } //3) No redundant router support @@ -1002,12 +1006,11 @@ public class VpcManagerImpl implements VpcManager, Manager{ } } } - } @DB protected void validateNewVpcGuestNetwork(String cidr, String gateway, Account networkOwner, Vpc vpc, String networkDomain) { - + Transaction txn = Transaction.currentTxn(); txn.start(); Vpc locked = _vpcDao.acquireInLockTable(vpc.getId()); @@ -1087,6 +1090,10 @@ public class VpcManagerImpl implements VpcManager, Manager{ vpcElement = ((VpcProvider)_ntwkMgr.getElementImplementingProvider(Provider.VPCVirtualRouter.getName())); } + if (vpcElement == null) { + throw new CloudRuntimeException("Failed to initialize vpc element"); + } + return vpcElement; } @@ -1154,7 +1161,7 @@ public class VpcManagerImpl implements VpcManager, Manager{ // Verify input parameters Vpc vpc = getActiveVpc(vpcId); if (vpc == null) { - InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find Enabled VPC by id", null); + throw new InvalidParameterValueException("Unable to find Enabled VPC", null); } _accountMgr.checkAccess(caller, null, false, vpc); @@ -1220,7 +1227,7 @@ public class VpcManagerImpl implements VpcManager, Manager{ //Validate parameters Vpc vpc = getActiveVpc(vpcId); if (vpc == null) { - InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find Enabled VPC by id", null); + throw new InvalidParameterValueException("Unable to find Enabled VPC", null); } //allow only one private gateway per vpc @@ -1300,7 +1307,7 @@ public class VpcManagerImpl implements VpcManager, Manager{ @ActionEvent(eventType = EventTypes.EVENT_PRIVATE_GATEWAY_DELETE, eventDescription = "deleting private gateway") @DB public boolean deleteVpcPrivateGateway(long gatewayId) throws ConcurrentOperationException, ResourceUnavailableException { - + Transaction txn = Transaction.currentTxn(); txn.start(); VpcGatewayVO gatewayVO = _vpcGatewayDao.acquireInLockTable(gatewayId); @@ -1315,11 +1322,11 @@ public class VpcManagerImpl implements VpcManager, Manager{ throw new CloudRuntimeException("Can't delete private gateway " + gatewayVO + " as it has " + routeCount + " static routes applied. Remove the routes first"); } - + gatewayVO.setState(VpcGateway.State.Deleting); _vpcGatewayDao.update(gatewayVO.getId(), gatewayVO); s_logger.debug("Marked gateway " + gatewayVO + " with state " + VpcGateway.State.Deleting); - + txn.commit(); //1) delete the gateway on the backend @@ -1390,11 +1397,12 @@ public class VpcManagerImpl implements VpcManager, Manager{ Account caller = UserContext.current().getCaller(); List permittedAccounts = new ArrayList(); String state = cmd.getState(); + Long projectId = cmd.getProjectId(); Filter searchFilter = new Filter(VpcGatewayVO.class, "id", false, cmd.getStartIndex(), cmd.getPageSizeVal()); Ternary domainIdRecursiveListProject = new Ternary(domainId, isRecursive, null); - _accountMgr.buildACLSearchParameters(caller, null, accountName, null, permittedAccounts, domainIdRecursiveListProject, + _accountMgr.buildACLSearchParameters(caller, id, accountName, projectId, permittedAccounts, domainIdRecursiveListProject, listAll, false); domainId = domainIdRecursiveListProject.first(); isRecursive = domainIdRecursiveListProject.second(); @@ -1566,8 +1574,18 @@ public class VpcManagerImpl implements VpcManager, Manager{ if (!NetUtils.isValidCIDR(cidr)){ throw new InvalidParameterValueException("Invalid format for cidr " + cidr, null); } + + //validate the cidr + //1) CIDR should be outside of VPC cidr for guest networks + if (NetUtils.isNetworksOverlap(vpc.getCidr(), cidr)) { + throw new InvalidParameterValueException("CIDR should be outside of VPC cidr " + vpc.getCidr(), null); + } + + //2) CIDR should be outside of link-local cidr + if (NetUtils.isNetworksOverlap(vpc.getCidr(), NetUtils.getLinkLocalCIDR())) { + throw new InvalidParameterValueException("CIDR should be outside of link local cidr " + NetUtils.getLinkLocalCIDR(), null); + } - //TODO - check cidr for the conflicts Transaction txn = Transaction.currentTxn(); txn.start(); @@ -1599,10 +1617,11 @@ public class VpcManagerImpl implements VpcManager, Manager{ Account caller = UserContext.current().getCaller(); List permittedAccounts = new ArrayList(); Map tags = cmd.getTags(); + Long projectId = cmd.getProjectId(); Ternary domainIdRecursiveListProject = new Ternary(domainId, isRecursive, null); - _accountMgr.buildACLSearchParameters(caller, id, accountName, null, permittedAccounts, domainIdRecursiveListProject, + _accountMgr.buildACLSearchParameters(caller, id, accountName, projectId, permittedAccounts, domainIdRecursiveListProject, listAll, false); domainId = domainIdRecursiveListProject.first(); isRecursive = domainIdRecursiveListProject.second(); @@ -1734,7 +1753,7 @@ public class VpcManagerImpl implements VpcManager, Manager{ public VpcGateway getPrivateGatewayForVpc(long vpcId) { return _vpcGatewayDao.getPrivateGatewayForVpc(vpcId); } - + @DB @Override @ActionEvent(eventType = EventTypes.EVENT_NET_IP_ASSIGN, eventDescription = "associating Ip", async = true) @@ -1783,8 +1802,8 @@ public class VpcManagerImpl implements VpcManager, Manager{ return _ipAddressDao.findById(ipId); } - - + + @Override public void unassignIPFromVpcNetwork(long ipId, long networkId) { IPAddressVO ip = _ipAddressDao.findById(ipId); @@ -1818,13 +1837,13 @@ public class VpcManagerImpl implements VpcManager, Manager{ } s_logger.debug("Successfully released VPC ip address " + ip + " back to VPC pool "); } - + @Override public boolean ipUsedInVpc(IpAddress ip) { return (ip != null && ip.getVpcId() != null && (ip.isOneToOneNat() || !_firewallDao.listByIp(ip.getId()).isEmpty())); } - + @DB @Override public Network createVpcGuestNetwork(long ntwkOffId, String name, String displayText, String gateway, @@ -1835,20 +1854,18 @@ public class VpcManagerImpl implements VpcManager, Manager{ Vpc vpc = getActiveVpc(vpcId); if (vpc == null) { - InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find Enabled VPC ", null); - ex.addProxyObject("vpc", vpcId, "VPC"); - throw ex; + throw new InvalidParameterValueException("Unable to find Enabled VPC ", null); } _accountMgr.checkAccess(caller, null, false, vpc); - + if (networkDomain == null) { networkDomain = vpc.getNetworkDomain(); } - + if (vpc.getZoneId() != zoneId) { throw new InvalidParameterValueException("New network doesn't belong to vpc zone", null); } - + //1) Validate if network can be created for VPC validateNtkwOffForVpc(ntwkOffId, cidr, networkDomain, owner, vpc, null, gateway); @@ -1858,12 +1875,12 @@ public class VpcManagerImpl implements VpcManager, Manager{ return guestNetwork; } - - + + protected IPAddressVO getExistingSourceNatInVpc(long ownerId, long vpcId) { List addrs = listPublicIpsAssignedToVpc(ownerId, true, vpcId); - + IPAddressVO sourceNatIp = null; if (addrs.isEmpty()) { return null; @@ -1882,7 +1899,7 @@ public class VpcManagerImpl implements VpcManager, Manager{ return sourceNatIp; } - + protected List listPublicIpsAssignedToVpc(long accountId, Boolean sourceNat, long vpcId) { SearchCriteria sc = IpAddressSearch.create(); sc.setParameters("accountId", accountId); @@ -1895,8 +1912,8 @@ public class VpcManagerImpl implements VpcManager, Manager{ return _ipAddressDao.search(sc, null); } - - + + @Override public PublicIp assignSourceNatIpAddressToVpc(Account owner, Vpc vpc) throws InsufficientAddressCapacityException, ConcurrentOperationException { long dcId = vpc.getZoneId(); @@ -1928,7 +1945,7 @@ public class VpcManagerImpl implements VpcManager, Manager{ Vpc vpc = getVpc(network.getVpcId()); validateNtkwOffForVpc(ntwkOffId, null, null, null, vpc, networkId, null); } - + return _ntwkMgr.updateGuestNetwork(networkId, name, displayText, callerAccount, callerUser, domainSuffix, ntwkOffId, changeCidr); } diff --git a/server/src/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java b/server/src/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java index b5fa4ea7aab..ba4d4009317 100755 --- a/server/src/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java +++ b/server/src/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java @@ -49,7 +49,6 @@ import com.cloud.network.dao.IPAddressDao; import com.cloud.network.dao.RemoteAccessVpnDao; import com.cloud.network.dao.VpnUserDao; import com.cloud.network.element.RemoteAccessVPNServiceProvider; -import com.cloud.network.router.VirtualNetworkApplianceManager; import com.cloud.network.rules.FirewallManager; import com.cloud.network.rules.FirewallRule; import com.cloud.network.rules.FirewallRule.Purpose; @@ -87,7 +86,6 @@ public class RemoteAccessVpnManagerImpl implements RemoteAccessVpnService, Manag @Inject VpnUserDao _vpnUsersDao; @Inject RemoteAccessVpnDao _remoteAccessVpnDao; @Inject IPAddressDao _ipAddressDao; - @Inject VirtualNetworkApplianceManager _routerMgr; @Inject AccountManager _accountMgr; @Inject DomainManager _domainMgr; @Inject NetworkManager _networkMgr; diff --git a/server/src/com/cloud/network/vpn/Site2SiteVpnManager.java b/server/src/com/cloud/network/vpn/Site2SiteVpnManager.java index 922ac2c4b6e..73dbef56ff1 100644 --- a/server/src/com/cloud/network/vpn/Site2SiteVpnManager.java +++ b/server/src/com/cloud/network/vpn/Site2SiteVpnManager.java @@ -11,4 +11,5 @@ public interface Site2SiteVpnManager extends Site2SiteVpnService { void markDisconnectVpnConnByVpc(long vpcId); List getConnectionsForRouter(DomainRouterVO router); boolean deleteCustomerGatewayByAccount(long accountId); + void reconnectDisconnectedVpnByVpc(Long vpcId); } diff --git a/server/src/com/cloud/network/vpn/Site2SiteVpnManagerImpl.java b/server/src/com/cloud/network/vpn/Site2SiteVpnManagerImpl.java index 45bfeb5ec18..b503d9f68c7 100644 --- a/server/src/com/cloud/network/vpn/Site2SiteVpnManagerImpl.java +++ b/server/src/com/cloud/network/vpn/Site2SiteVpnManagerImpl.java @@ -49,11 +49,14 @@ import com.cloud.projects.Project.ListProjectResourcesCriteria; import com.cloud.user.Account; import com.cloud.user.AccountManager; import com.cloud.user.UserContext; +import com.cloud.user.UserStatisticsVO; import com.cloud.user.dao.AccountDao; +import com.cloud.user.dao.UserStatisticsDao; import com.cloud.utils.IdentityProxy; import com.cloud.utils.Ternary; import com.cloud.utils.component.Inject; import com.cloud.utils.component.Manager; +import com.cloud.utils.db.DB; import com.cloud.utils.db.Filter; import com.cloud.utils.db.GenericDao; import com.cloud.utils.db.JoinBuilder; @@ -76,7 +79,7 @@ public class Site2SiteVpnManagerImpl implements Site2SiteVpnManager, Manager { @Inject AccountDao _accountDao; @Inject VpcManager _vpcMgr; @Inject AccountManager _accountMgr; - + @Inject UserStatisticsDao _statsDao = null; String _name; @Override @@ -128,6 +131,13 @@ public class Site2SiteVpnManagerImpl implements Site2SiteVpnManager, Manager { Site2SiteVpnGatewayVO gw = new Site2SiteVpnGatewayVO(owner.getAccountId(), owner.getDomainId(), ips.get(0).getId(), vpcId); _vpnGatewayDao.persist(gw); + UserStatisticsVO stats = _statsDao.findBy(owner.getAccountId(), vpc.getZoneId(), ips.get(0).getSourceNetworkId(), + ips.get(0).getAddress().toString(), gw.getId(), "VPNGateway"); + if (stats == null) { + stats = new UserStatisticsVO(owner.getAccountId(), vpc.getZoneId(), ips.get(0).getAddress().toString(), gw.getId(), + "VPNGateway", ips.get(0).getSourceNetworkId()); + _statsDao.persist(stats); + } return gw; } @@ -218,6 +228,10 @@ public class Site2SiteVpnManagerImpl implements Site2SiteVpnManager, Manager { throw new InvalidParameterValueException("Unable to find specified Site to Site VPN gateway by id", null); } _accountMgr.checkAccess(caller, null, false, vpnGateway); + + if (customerGateway.getAccountId() != vpnGateway.getAccountId() || customerGateway.getDomainId() != vpnGateway.getDomainId()) { + throw new InvalidParameterValueException("VPN connection can only be esitablished between same account's VPN gateway and customer gateway!", null); + } if (_vpnConnectionDao.findByVpnGatewayIdAndCustomerGatewayId(vpnGatewayId, customerGatewayId) != null) { List idList = new ArrayList(); @@ -251,30 +265,39 @@ public class Site2SiteVpnManagerImpl implements Site2SiteVpnManager, Manager { } @Override + @DB public Site2SiteVpnConnection startVpnConnection(long id) throws ResourceUnavailableException { - Site2SiteVpnConnectionVO conn = _vpnConnectionDao.findById(id); - if (conn.getState() != State.Pending && conn.getState() != State.Disconnected) { - List idList = new ArrayList(); - idList.add(new IdentityProxy(conn, id, "connectionId")); - throw new InvalidParameterValueException("Site to site VPN connection with specified connectionId not in correct state(pending or disconnected) to process!", idList); + Site2SiteVpnConnectionVO conn = _vpnConnectionDao.acquireInLockTable(id); + if (conn == null) { + throw new CloudRuntimeException("Unable to acquire lock on " + conn); } + try { + if (conn.getState() != State.Pending && conn.getState() != State.Disconnected) { + List idList = new ArrayList(); + idList.add(new IdentityProxy(conn, id, "connectionId")); + throw new InvalidParameterValueException("Site to site VPN connection with specified connectionId not in correct state(pending or disconnected) to process!", idList); + } - conn.setState(State.Pending); - _vpnConnectionDao.persist(conn); - List elements = _networkMgr.getSite2SiteVpnElements(); - boolean result = true; - for (Site2SiteVpnServiceProvider element : elements) { - result = result & element.startSite2SiteVpn(conn); - } - - if (result) { - conn.setState(State.Connected); + conn.setState(State.Pending); _vpnConnectionDao.persist(conn); - return conn; + + List elements = _networkMgr.getSite2SiteVpnElements(); + boolean result = true; + for (Site2SiteVpnServiceProvider element : elements) { + result = result & element.startSite2SiteVpn(conn); + } + + if (result) { + conn.setState(State.Connected); + _vpnConnectionDao.persist(conn); + return conn; + } + conn.setState(State.Error); + _vpnConnectionDao.persist(conn); + throw new ResourceUnavailableException("Failed to apply site-to-site VPN", Site2SiteVpnConnection.class, id); + } finally { + _vpnConnectionDao.releaseFromLockTable(conn.getId()); } - conn.setState(State.Error); - _vpnConnectionDao.persist(conn); - throw new ResourceUnavailableException("Failed to apply site-to-site VPN", Site2SiteVpnConnection.class, id); } @Override @@ -439,26 +462,35 @@ public class Site2SiteVpnManagerImpl implements Site2SiteVpnManager, Manager { return true; } + @DB private void stopVpnConnection(Long id) throws ResourceUnavailableException { - Site2SiteVpnConnectionVO conn = _vpnConnectionDao.findById(id); - if (conn.getState() != State.Connected && conn.getState() != State.Error) { - List idList = new ArrayList(); - idList.add(new IdentityProxy(conn, id, "vpnConnectionId")); - throw new InvalidParameterValueException("Site to site VPN connection with specified id is not in correct state(connected) to process disconnect!", idList); + Site2SiteVpnConnectionVO conn = _vpnConnectionDao.acquireInLockTable(id); + if (conn == null) { + throw new CloudRuntimeException("Unable to acquire lock on " + conn); } + try { + if (conn.getState() != State.Connected && conn.getState() != State.Error) { + List idList = new ArrayList(); + idList.add(new IdentityProxy(conn, id, "vpnConnectionId")); + throw new InvalidParameterValueException("Site to site VPN connection with specified id is not in correct state(connected) to process disconnect!", idList); + } - List elements = _networkMgr.getSite2SiteVpnElements(); - boolean result = true; - conn.setState(State.Disconnected); - _vpnConnectionDao.persist(conn); - for (Site2SiteVpnServiceProvider element : elements) { - result = result & element.stopSite2SiteVpn(conn); - } - - if (!result) { - conn.setState(State.Error); + conn.setState(State.Disconnected); _vpnConnectionDao.persist(conn); - throw new ResourceUnavailableException("Failed to apply site-to-site VPN", Site2SiteVpnConnection.class, id); + + List elements = _networkMgr.getSite2SiteVpnElements(); + boolean result = true; + for (Site2SiteVpnServiceProvider element : elements) { + result = result & element.stopSite2SiteVpn(conn); + } + + if (!result) { + conn.setState(State.Error); + _vpnConnectionDao.persist(conn); + throw new ResourceUnavailableException("Failed to apply site-to-site VPN", Site2SiteVpnConnection.class, id); + } + } finally { + _vpnConnectionDao.releaseFromLockTable(conn.getId()); } } @@ -641,15 +673,24 @@ public class Site2SiteVpnManagerImpl implements Site2SiteVpnManager, Manager { } @Override + @DB public void markDisconnectVpnConnByVpc(long vpcId) { List conns = _vpnConnectionDao.listByVpcId(vpcId); for (Site2SiteVpnConnectionVO conn : conns) { if (conn == null) { continue; } - if (conn.getState() == Site2SiteVpnConnection.State.Connected) { - conn.setState(Site2SiteVpnConnection.State.Disconnected); - _vpnConnectionDao.persist(conn); + Site2SiteVpnConnectionVO lock = _vpnConnectionDao.acquireInLockTable(conn.getId()); + if (lock == null) { + throw new CloudRuntimeException("Unable to acquire lock on " + conn); + } + try { + if (conn.getState() == Site2SiteVpnConnection.State.Connected) { + conn.setState(Site2SiteVpnConnection.State.Disconnected); + _vpnConnectionDao.persist(conn); + } + } finally { + _vpnConnectionDao.releaseFromLockTable(lock.getId()); } } } @@ -675,4 +716,22 @@ public class Site2SiteVpnManagerImpl implements Site2SiteVpnManager, Manager { } return result; } + + @Override + public void reconnectDisconnectedVpnByVpc(Long vpcId) { + List conns = _vpnConnectionDao.listByVpcId(vpcId); + for (Site2SiteVpnConnectionVO conn : conns) { + if (conn == null) { + continue; + } + if (conn.getState() == Site2SiteVpnConnection.State.Disconnected) { + try { + startVpnConnection(conn.getId()); + } catch (ResourceUnavailableException e) { + Site2SiteCustomerGatewayVO gw = _customerGatewayDao.findById(conn.getCustomerGatewayId()); + s_logger.warn("Site2SiteVpnManager: Fail to re-initiate VPN connection " + conn.getId() + " which connect to " + gw.getName()); + } + } + } + } } diff --git a/server/src/com/cloud/resource/ResourceManagerImpl.java b/server/src/com/cloud/resource/ResourceManagerImpl.java index 942247ed2e4..cfc3a1dbbd5 100755 --- a/server/src/com/cloud/resource/ResourceManagerImpl.java +++ b/server/src/com/cloud/resource/ResourceManagerImpl.java @@ -611,7 +611,7 @@ public class ResourceManagerImpl implements ResourceManager, ResourceService, Ma if (clusterId != null) { ClusterVO cluster = _clusterDao.findById(clusterId); if (cluster == null) { - InvalidParameterValueException ex = new InvalidParameterValueException("can not find cluster by Id", null); + throw new InvalidParameterValueException("can not find cluster", null); } else { if (cluster.getGuid() == null) { List hosts = listAllHostsInCluster(clusterId); diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index 368dad87d71..a4edb3b3777 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -1303,9 +1303,9 @@ public class ManagementServerImpl implements ManagementServer { }// If ISO requested then it should be ISO. if (isIso && template.getFormat() != ImageFormat.ISO) { s_logger.error("Template Id " + templateId + " is not an ISO"); - InvalidParameterValueException ex = new InvalidParameterValueException("Specified Template Id is not an ISO", null); - ex.addProxyObject(template, templateId, "templateId"); - throw ex; + List idList = new ArrayList(); + idList.add(new IdentityProxy(template, templateId, "templateId")); + throw new InvalidParameterValueException("Specified Template Id is not an ISO", idList); }// If ISO not requested then it shouldn't be an ISO. if (!isIso && template.getFormat() == ImageFormat.ISO) { s_logger.error("Incorrect format of the template id " + templateId); diff --git a/server/src/com/cloud/servlet/ConsoleProxyServlet.java b/server/src/com/cloud/servlet/ConsoleProxyServlet.java index 31320d3f69a..094726623f2 100644 --- a/server/src/com/cloud/servlet/ConsoleProxyServlet.java +++ b/server/src/com/cloud/servlet/ConsoleProxyServlet.java @@ -175,7 +175,8 @@ public class ConsoleProxyServlet extends HttpServlet { return; } - String rootUrl = _ms.getConsoleAccessUrlRoot(vmId); + String rootUrl = _ms.getConsoleAccessUrlRoot(vmId); + if(rootUrl == null) { sendResponse(resp, ""); return; @@ -226,7 +227,9 @@ public class ConsoleProxyServlet extends HttpServlet { return; } - String rootUrl = _ms.getConsoleAccessUrlRoot(vmId); + String rootUrl = _ms.getConsoleAccessUrlRoot(vmId); + //String rootUrl = "http://localhost:8099"; + if(rootUrl == null) { sendResponse(resp, "

Console access will be ready in a few minutes. Please try it again later.

"); return; @@ -322,6 +325,7 @@ public class ConsoleProxyServlet extends HttpServlet { ConsoleProxyPasswordBasedEncryptor encryptor = new ConsoleProxyPasswordBasedEncryptor(_ms.getHashKey()); ConsoleProxyClientParam param = new ConsoleProxyClientParam(); + param.setClientHostAddress(parsedHostInfo.first()); param.setClientHostPort(portInfo.second()); param.setClientHostPassword(sid); @@ -331,9 +335,9 @@ public class ConsoleProxyServlet extends HttpServlet { param.setClientTunnelUrl(parsedHostInfo.second()); param.setClientTunnelSession(parsedHostInfo.third()); } - - sb.append("/ajax?token=" + encryptor.encryptObject(ConsoleProxyClientParam.class, param)); - sb.append("&w=").append(w).append("&h=").append(h); + s_logger.debug("Thumbnail url params" + param.toString()); + sb.append("/ajaximg?token=" + encryptor.encryptObject(ConsoleProxyClientParam.class, param)); + sb.append("&w=").append(w).append("&h=").append(h).append("&key=0"); if(s_logger.isDebugEnabled()) { s_logger.debug("Compose thumbnail url: " + sb.toString()); diff --git a/server/src/com/cloud/storage/StorageManagerImpl.java b/server/src/com/cloud/storage/StorageManagerImpl.java index fe4b05eb823..7df4802feda 100755 --- a/server/src/com/cloud/storage/StorageManagerImpl.java +++ b/server/src/com/cloud/storage/StorageManagerImpl.java @@ -121,7 +121,6 @@ import com.cloud.host.dao.HostDao; import com.cloud.hypervisor.Hypervisor.HypervisorType; import com.cloud.hypervisor.HypervisorGuruManager; import com.cloud.network.NetworkManager; -import com.cloud.network.router.VirtualNetworkApplianceManager; import com.cloud.org.Grouping; import com.cloud.org.Grouping.AllocationState; import com.cloud.projects.Project.ListProjectResourcesCriteria; @@ -293,8 +292,6 @@ public class StorageManagerImpl implements StorageManager, Manager, ClusterManag @Inject protected ClusterDao _clusterDao; @Inject - protected VirtualNetworkApplianceManager _routerMgr; - @Inject protected UsageEventDao _usageEventDao; @Inject protected VirtualMachineManager _vmMgr; @@ -642,6 +639,9 @@ public class StorageManagerImpl implements StorageManager, Manager, ClusterManag Pair volumeDetails = createVolumeFromSnapshot(volume, snapshot); if (volumeDetails != null) { createdVolume = volumeDetails.first(); + UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_VOLUME_CREATE, createdVolume.getAccountId(), createdVolume.getDataCenterId(), createdVolume.getId(), createdVolume.getName(), + createdVolume.getDiskOfferingId(), null, createdVolume.getSize()); + _usageEventDao.persist(usageEvent); } return createdVolume; } @@ -1996,8 +1996,11 @@ public class StorageManagerImpl implements StorageManager, Manager, ClusterManag volume.setDomainId((caller == null) ? Domain.ROOT_DOMAIN : caller.getDomainId()); volume = _volsDao.persist(volume); - UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_VOLUME_CREATE, volume.getAccountId(), volume.getDataCenterId(), volume.getId(), volume.getName(), diskOfferingId, null, size); - _usageEventDao.persist(usageEvent); + if(cmd.getSnapshotId() == null){ + //for volume created from snapshot, create usage event after volume creation + UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_VOLUME_CREATE, volume.getAccountId(), volume.getDataCenterId(), volume.getId(), volume.getName(), diskOfferingId, null, size); + _usageEventDao.persist(usageEvent); + } UserContext.current().setEventDetails("Volume Id: " + volume.getId()); @@ -3223,7 +3226,8 @@ public class StorageManagerImpl implements StorageManager, Manager, ClusterManag if (s_logger.isDebugEnabled()) { s_logger.debug("Mismatch in storage pool " + assignedPool + " assigned by deploymentPlanner and the one associated with volume " + vol); } - if (vm.getServiceOffering().getUseLocalStorage()) + DiskOfferingVO diskOffering = _diskOfferingDao.findById(vol.getDiskOfferingId()); + if (diskOffering.getUseLocalStorage()) { if (s_logger.isDebugEnabled()) { s_logger.debug("Local volume " + vol + " will be recreated on storage pool " + assignedPool + " assigned by deploymentPlanner"); @@ -3234,10 +3238,12 @@ public class StorageManagerImpl implements StorageManager, Manager, ClusterManag s_logger.debug("Shared volume " + vol + " will be migrated on storage pool " + assignedPool + " assigned by deploymentPlanner"); } try { - Volume migratedVol = migrateVolume(vol.getId(), assignedPool.getId()); - vm.addDisk(new VolumeTO(migratedVol, assignedPool)); + List volumesToMigrate = new ArrayList(); + volumesToMigrate.add(vol); + migrateVolumes(volumesToMigrate, assignedPool); + vm.addDisk(new VolumeTO(vol, assignedPool)); } catch (ConcurrentOperationException e) { - throw new StorageUnavailableException("Volume migration failed for " + vol, Volume.class, vol.getId()); + throw new CloudRuntimeException("Migration of volume " + vol + " to storage pool " + assignedPool + " failed", e); } } } else { diff --git a/server/src/com/cloud/upgrade/dao/Upgrade218to22.java b/server/src/com/cloud/upgrade/dao/Upgrade218to22.java index c39074bd109..48d063506c0 100644 --- a/server/src/com/cloud/upgrade/dao/Upgrade218to22.java +++ b/server/src/com/cloud/upgrade/dao/Upgrade218to22.java @@ -37,7 +37,7 @@ import com.cloud.consoleproxy.ConsoleProxyManager; import com.cloud.event.EventTypes; import com.cloud.event.EventVO; import com.cloud.event.UsageEventVO; -import com.cloud.network.router.VirtualNetworkApplianceManager; +import com.cloud.network.router.VpcVirtualNetworkApplianceManager; import com.cloud.storage.secondary.SecondaryStorageVmManager; import com.cloud.utils.DateUtil; import com.cloud.utils.NumbersUtil; @@ -1404,7 +1404,7 @@ public class Upgrade218to22 implements DbUpgrade { pstmt.close(); int proxyRamSize = NumbersUtil.parseInt(getConfigValue(conn, "consoleproxy.ram.size"), ConsoleProxyManager.DEFAULT_PROXY_VM_RAMSIZE); - int domrRamSize = NumbersUtil.parseInt(getConfigValue(conn, "router.ram.size"), VirtualNetworkApplianceManager.DEFAULT_ROUTER_VM_RAMSIZE); + int domrRamSize = NumbersUtil.parseInt(getConfigValue(conn, "router.ram.size"), VpcVirtualNetworkApplianceManager.DEFAULT_ROUTER_VM_RAMSIZE); int ssvmRamSize = NumbersUtil.parseInt(getConfigValue(conn, "secstorage.vm.ram.size"), SecondaryStorageVmManager.DEFAULT_SS_VM_RAMSIZE); pstmt = conn @@ -1563,7 +1563,7 @@ public class Upgrade218to22 implements DbUpgrade { pstmt.close(); int proxyCpuMhz = NumbersUtil.parseInt(getConfigValue(conn, "consoleproxy.cpu.mhz"), ConsoleProxyManager.DEFAULT_PROXY_VM_CPUMHZ); - int domrCpuMhz = NumbersUtil.parseInt(getConfigValue(conn, "router.cpu.mhz"), VirtualNetworkApplianceManager.DEFAULT_ROUTER_CPU_MHZ); + int domrCpuMhz = NumbersUtil.parseInt(getConfigValue(conn, "router.cpu.mhz"), VpcVirtualNetworkApplianceManager.DEFAULT_ROUTER_CPU_MHZ); int ssvmCpuMhz = NumbersUtil.parseInt(getConfigValue(conn, "secstorage.vm.cpu.mhz"), SecondaryStorageVmManager.DEFAULT_SS_VM_CPUMHZ); pstmt = conn diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java index 83de4d63b8d..4e78b1e3f52 100755 --- a/server/src/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/com/cloud/vm/UserVmManagerImpl.java @@ -525,7 +525,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager return maxDataVolumesSupported.intValue(); } - + @Override @ActionEvent(eventType = EventTypes.EVENT_VOLUME_ATTACH, eventDescription = "attaching volume", async = true) public Volume attachVolumeToVM(AttachVolumeCmd command) { @@ -2549,6 +2549,9 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager private void validateUserData(String userData) { byte[] decodedUserData = null; if (userData != null) { + if ( !userData.matches("^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$")) { + throw new InvalidParameterValueException("User data is not base64 encoded", null); + } if (userData.length() >= 2 * MAX_USER_DATA_LENGTH_BYTES) { throw new InvalidParameterValueException("User data is too long", null); } @@ -2928,9 +2931,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager // Verify input parameters UserVmVO vm = _vmDao.findById(vmId); if (vm == null || vm.getRemoved() != null) { - InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find a virtual machine with specified vmId", null); - ex.addProxyObject(vm, vmId, "vmId"); - throw ex; + throw new InvalidParameterValueException("Unable to find a virtual machine with specified vmId", null); } if (vm.getState() == State.Destroyed || vm.getState() == State.Expunging) { @@ -3138,11 +3139,11 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager if (id != null) { sc.setParameters("id", id); } - + if (templateId != null) { sc.setParameters("templateId", templateId); } - + if (isoId != null) { sc.setParameters("isoId", isoId); } @@ -3314,9 +3315,9 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager if (s_logger.isDebugEnabled()) { s_logger.debug("VM is not Running, unable to migrate the vm " + vm); } - InvalidParameterValueException ex = new InvalidParameterValueException("VM is not Running, unable to migrate the vm with specified id", null); - ex.addProxyObject(vm, vmId, "vmId"); - throw ex; + List idList = new ArrayList(); + idList.add(new IdentityProxy(vm, vmId, "vmId")); + throw new InvalidParameterValueException("VM is not Running, unable to migrate the vm with specified id", idList); } if (!vm.getHypervisorType().equals(HypervisorType.XenServer) && !vm.getHypervisorType().equals(HypervisorType.VMware) && !vm.getHypervisorType().equals(HypervisorType.KVM) && !vm.getHypervisorType().equals(HypervisorType.Ovm)) { if (s_logger.isDebugEnabled()) { @@ -3396,9 +3397,9 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager } //don't allow to move the vm from the project if (oldAccount.getType() == Account.ACCOUNT_TYPE_PROJECT) { - InvalidParameterValueException ex = new InvalidParameterValueException("Specified Vm id belongs to the project and can't be moved", null); - ex.addProxyObject(vm, cmd.getVmId(), "vmId"); - throw ex; + List idList = new ArrayList(); + idList.add(new IdentityProxy(vm, cmd.getVmId(), "vmId")); + throw new InvalidParameterValueException("Specified Vm id belongs to the project and can't be moved", idList); } Account newAccount = _accountService.getActiveAccountByName(cmd.getAccountName(), cmd.getDomainId()); if (newAccount == null || newAccount.getType() == Account.ACCOUNT_TYPE_PROJECT) { @@ -3425,8 +3426,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager idList.add(new IdentityProxy(oldAccount, oldAccount.getAccountId(), "accountId")); throw new InvalidParameterValueException("The account with the specified id should be same domain for moving VM between two accounts.", idList); } - - + // don't allow to move the vm if there are existing PF/LB/Static Nat rules, or vm is assigned to static Nat ip List pfrules = _portForwardingDao.listByVm(cmd.getVmId()); if (pfrules != null && pfrules.size() > 0){ @@ -3456,6 +3456,9 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager //VV 2: check if account/domain is with in resource limits to create a new vm _resourceLimitMgr.checkResourceLimit(newAccount, ResourceType.user_vm); + //VV 3: check if volumes are with in resource limits + _resourceLimitMgr.checkResourceLimit(newAccount, ResourceType.volume, _volsDao.findByInstance(cmd.getVmId()).size()); + // VV 4: Check if new owner can use the vm template VirtualMachineTemplate template = _templateDao.findById(vm.getTemplateId()); if (!template.isPublicTemplate()) { @@ -3680,9 +3683,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager long vmId = cmd.getVmId(); UserVmVO vm = _vmDao.findById(vmId); if (vm == null) { - InvalidParameterValueException ex = new InvalidParameterValueException("Cann not find VM by ID ", null); - ex.addProxyObject(vm, vmId, "vmId"); - throw ex; + throw new InvalidParameterValueException("Cann not find VM", null); } Account owner = _accountDao.findById(vm.getAccountId()); diff --git a/server/src/com/cloud/vm/VirtualMachineManagerImpl.java b/server/src/com/cloud/vm/VirtualMachineManagerImpl.java index f63f182c7d0..1931a36d0be 100755 --- a/server/src/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/server/src/com/cloud/vm/VirtualMachineManagerImpl.java @@ -1,2560 +1,2560 @@ -// 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.vm; - -import java.net.URI; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Date; -import java.util.Enumeration; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.UUID; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; - -import javax.ejb.Local; -import javax.naming.ConfigurationException; - -import org.apache.log4j.Logger; - -import com.cloud.agent.AgentManager; -import com.cloud.agent.AgentManager.OnError; -import com.cloud.agent.Listener; -import com.cloud.agent.api.AgentControlAnswer; -import com.cloud.agent.api.AgentControlCommand; -import com.cloud.agent.api.Answer; -import com.cloud.agent.api.CheckVirtualMachineAnswer; -import com.cloud.agent.api.CheckVirtualMachineCommand; -import com.cloud.agent.api.ClusterSyncAnswer; -import com.cloud.agent.api.ClusterSyncCommand; -import com.cloud.agent.api.Command; -import com.cloud.agent.api.MigrateAnswer; -import com.cloud.agent.api.MigrateCommand; -import com.cloud.agent.api.PingRoutingCommand; -import com.cloud.agent.api.PrepareForMigrationAnswer; -import com.cloud.agent.api.PrepareForMigrationCommand; -import com.cloud.agent.api.RebootAnswer; -import com.cloud.agent.api.RebootCommand; -import com.cloud.agent.api.StartAnswer; -import com.cloud.agent.api.StartCommand; -import com.cloud.agent.api.StartupCommand; -import com.cloud.agent.api.StartupRoutingCommand; -import com.cloud.agent.api.StartupRoutingCommand.VmState; -import com.cloud.agent.api.StopAnswer; -import com.cloud.agent.api.StopCommand; -import com.cloud.agent.api.to.NicTO; -import com.cloud.agent.api.to.VirtualMachineTO; -import com.cloud.agent.manager.Commands; -import com.cloud.agent.manager.allocator.HostAllocator; -import com.cloud.alert.AlertManager; -import com.cloud.capacity.CapacityManager; -import com.cloud.cluster.ClusterManager; -import com.cloud.cluster.StackMaid; -import com.cloud.configuration.Config; -import com.cloud.configuration.ConfigurationManager; -import com.cloud.configuration.dao.ConfigurationDao; -import com.cloud.consoleproxy.ConsoleProxyManager; -import com.cloud.dc.DataCenter; -import com.cloud.dc.DataCenterVO; -import com.cloud.dc.HostPodVO; -import com.cloud.dc.dao.DataCenterDao; -import com.cloud.dc.dao.HostPodDao; -import com.cloud.deploy.DataCenterDeployment; -import com.cloud.deploy.DeployDestination; -import com.cloud.deploy.DeploymentPlan; -import com.cloud.deploy.DeploymentPlanner; -import com.cloud.deploy.DeploymentPlanner.ExcludeList; -import com.cloud.domain.dao.DomainDao; -import com.cloud.exception.AgentUnavailableException; -import com.cloud.exception.ConcurrentOperationException; -import com.cloud.exception.ConnectionException; -import com.cloud.exception.InsufficientAddressCapacityException; -import com.cloud.exception.InsufficientCapacityException; -import com.cloud.exception.InsufficientServerCapacityException; -import com.cloud.exception.InsufficientVirtualNetworkCapcityException; -import com.cloud.exception.InvalidParameterValueException; -import com.cloud.exception.ManagementServerException; -import com.cloud.exception.OperationTimedoutException; -import com.cloud.exception.ResourceUnavailableException; -import com.cloud.exception.VirtualMachineMigrationException; -import com.cloud.ha.HighAvailabilityManager; -import com.cloud.ha.HighAvailabilityManager.WorkType; -import com.cloud.host.Host; -import com.cloud.host.HostVO; -import com.cloud.host.Status; -import com.cloud.host.dao.HostDao; -import com.cloud.hypervisor.Hypervisor.HypervisorType; -import com.cloud.hypervisor.HypervisorGuru; -import com.cloud.hypervisor.HypervisorGuruManager; -import com.cloud.network.Network; -import com.cloud.network.NetworkManager; -import com.cloud.network.NetworkVO; -import com.cloud.network.dao.NetworkDao; -import com.cloud.offering.ServiceOffering; -import com.cloud.org.Cluster; -import com.cloud.resource.ResourceManager; -import com.cloud.service.ServiceOfferingVO; -import com.cloud.service.dao.ServiceOfferingDao; -import com.cloud.storage.DiskOfferingVO; -import com.cloud.storage.Storage.ImageFormat; -import com.cloud.storage.StorageManager; -import com.cloud.storage.StoragePool; -import com.cloud.storage.StoragePoolVO; -import com.cloud.storage.VMTemplateVO; -import com.cloud.storage.Volume; -import com.cloud.storage.Volume.Type; -import com.cloud.storage.VolumeVO; -import com.cloud.storage.dao.GuestOSCategoryDao; -import com.cloud.storage.dao.GuestOSDao; -import com.cloud.storage.dao.StoragePoolDao; -import com.cloud.storage.dao.VMTemplateDao; -import com.cloud.storage.dao.VolumeDao; -import com.cloud.user.Account; -import com.cloud.user.AccountManager; -import com.cloud.user.User; -import com.cloud.user.dao.AccountDao; -import com.cloud.user.dao.UserDao; -import com.cloud.utils.Journal; -import com.cloud.utils.NumbersUtil; -import com.cloud.utils.Pair; -import com.cloud.utils.Ternary; -import com.cloud.utils.component.Adapters; -import com.cloud.utils.component.ComponentLocator; -import com.cloud.utils.component.Inject; -import com.cloud.utils.concurrency.NamedThreadFactory; -import com.cloud.utils.db.DB; -import com.cloud.utils.db.GlobalLock; -import com.cloud.utils.db.Transaction; -import com.cloud.utils.exception.CloudRuntimeException; -import com.cloud.utils.exception.ExecutionException; -import com.cloud.utils.fsm.NoTransitionException; -import com.cloud.utils.fsm.StateMachine2; -import com.cloud.vm.ItWorkVO.Step; -import com.cloud.vm.VirtualMachine.Event; -import com.cloud.vm.VirtualMachine.State; -import com.cloud.vm.dao.ConsoleProxyDao; -import com.cloud.vm.dao.DomainRouterDao; -import com.cloud.vm.dao.NicDao; -import com.cloud.vm.dao.SecondaryStorageVmDao; -import com.cloud.vm.dao.UserVmDao; -import com.cloud.vm.dao.VMInstanceDao; - -@Local(value = VirtualMachineManager.class) -public class VirtualMachineManagerImpl implements VirtualMachineManager, Listener { - private static final Logger s_logger = Logger.getLogger(VirtualMachineManagerImpl.class); - - String _name; - @Inject - protected StorageManager _storageMgr; - @Inject - protected NetworkManager _networkMgr; - @Inject - protected AgentManager _agentMgr; - @Inject - protected VMInstanceDao _vmDao; - @Inject - protected ServiceOfferingDao _offeringDao; - @Inject - protected VMTemplateDao _templateDao; - @Inject - protected UserDao _userDao; - @Inject - protected AccountDao _accountDao; - @Inject - protected DomainDao _domainDao; - @Inject - protected ClusterManager _clusterMgr; - @Inject - protected ItWorkDao _workDao; - @Inject - protected UserVmDao _userVmDao; - @Inject - protected DomainRouterDao _routerDao; - @Inject - protected ConsoleProxyDao _consoleDao; - @Inject - protected SecondaryStorageVmDao _secondaryDao; - @Inject - protected NicDao _nicsDao; - @Inject - protected AccountManager _accountMgr; - @Inject - protected HostDao _hostDao; - @Inject - protected AlertManager _alertMgr; - @Inject - protected GuestOSCategoryDao _guestOsCategoryDao; - @Inject - protected GuestOSDao _guestOsDao; - @Inject - protected VolumeDao _volsDao; - @Inject - protected ConsoleProxyManager _consoleProxyMgr; - @Inject - protected ConfigurationManager _configMgr; - @Inject - protected CapacityManager _capacityMgr; - @Inject - protected HighAvailabilityManager _haMgr; - @Inject - protected HostPodDao _podDao; - @Inject - protected DataCenterDao _dcDao; - @Inject - protected StoragePoolDao _storagePoolDao; - @Inject - protected HypervisorGuruManager _hvGuruMgr; - @Inject - protected NetworkDao _networkDao; - - @Inject(adapter = DeploymentPlanner.class) - protected Adapters _planners; - - @Inject(adapter = HostAllocator.class) - protected Adapters _hostAllocators; - - @Inject - protected ResourceManager _resourceMgr; - - Map> _vmGurus = new HashMap>(); - protected StateMachine2 _stateMachine; - - ScheduledExecutorService _executor = null; - protected int _operationTimeout; - - protected int _retry; - protected long _nodeId; - protected long _cleanupWait; - protected long _cleanupInterval; - protected long _cancelWait; - protected long _opWaitInterval; - protected int _lockStateRetry; - protected boolean _forceStop; - - @Override - public void registerGuru(VirtualMachine.Type type, VirtualMachineGuru guru) { - synchronized (_vmGurus) { - _vmGurus.put(type, guru); - } - } - - @Override - @DB - public T allocate(T vm, VMTemplateVO template, ServiceOfferingVO serviceOffering, Pair rootDiskOffering, - List> dataDiskOfferings, List> networks, Map params, DeploymentPlan plan, - HypervisorType hyperType, Account owner) throws InsufficientCapacityException { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Allocating entries for VM: " + vm); - } - - VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vm, template, serviceOffering, owner, params); - - vm.setDataCenterId(plan.getDataCenterId()); - if (plan.getPodId() != null) { - vm.setPodId(plan.getPodId()); - } - assert (plan.getClusterId() == null && plan.getPoolId() == null) : "We currently don't support cluster and pool preset yet"; - - @SuppressWarnings("unchecked") - VirtualMachineGuru guru = (VirtualMachineGuru) _vmGurus.get(vm.getType()); - - Transaction txn = Transaction.currentTxn(); - txn.start(); - vm = guru.persist(vm); - - if (s_logger.isDebugEnabled()) { - s_logger.debug("Allocating nics for " + vm); - } - - try { - _networkMgr.allocate(vmProfile, networks); - } catch (ConcurrentOperationException e) { - throw new CloudRuntimeException("Concurrent operation while trying to allocate resources for the VM", e); - } - - if (dataDiskOfferings == null) { - dataDiskOfferings = new ArrayList>(0); - } - - if (s_logger.isDebugEnabled()) { - s_logger.debug("Allocaing disks for " + vm); - } - - if (template.getFormat() == ImageFormat.ISO) { - _storageMgr.allocateRawVolume(Type.ROOT, "ROOT-" + vm.getId(), rootDiskOffering.first(), rootDiskOffering.second(), vm, owner); - } else if (template.getFormat() == ImageFormat.BAREMETAL) { - // Do nothing - } else { - _storageMgr.allocateTemplatedVolume(Type.ROOT, "ROOT-" + vm.getId(), rootDiskOffering.first(), template, vm, owner); - } - - for (Pair offering : dataDiskOfferings) { - _storageMgr.allocateRawVolume(Type.DATADISK, "DATA-" + vm.getId(), offering.first(), offering.second(), vm, owner); - } - - txn.commit(); - if (s_logger.isDebugEnabled()) { - s_logger.debug("Allocation completed for VM: " + vm); - } - - return vm; - } - - @Override - public T allocate(T vm, VMTemplateVO template, ServiceOfferingVO serviceOffering, Long rootSize, Pair dataDiskOffering, - List> networks, DeploymentPlan plan, HypervisorType hyperType, Account owner) throws InsufficientCapacityException { - List> diskOfferings = new ArrayList>(1); - if (dataDiskOffering != null) { - diskOfferings.add(dataDiskOffering); - } - return allocate(vm, template, serviceOffering, new Pair(serviceOffering, rootSize), diskOfferings, networks, null, plan, hyperType, owner); - } - - @Override - public T allocate(T vm, VMTemplateVO template, ServiceOfferingVO serviceOffering, List> networks, DeploymentPlan plan, - HypervisorType hyperType, Account owner) throws InsufficientCapacityException { - return allocate(vm, template, serviceOffering, new Pair(serviceOffering, null), null, networks, null, plan, hyperType, owner); - } - - @SuppressWarnings("unchecked") - private VirtualMachineGuru getVmGuru(T vm) { - return (VirtualMachineGuru) _vmGurus.get(vm.getType()); - } - - @SuppressWarnings("unchecked") - private VirtualMachineGuru getBareMetalVmGuru(T vm) { - return (VirtualMachineGuru) _vmGurus.get(VirtualMachine.Type.UserBareMetal); - } - - @Override - public boolean expunge(T vm, User caller, Account account) throws ResourceUnavailableException { - try { - if (advanceExpunge(vm, caller, account)) { - // Mark vms as removed - remove(vm, caller, account); - return true; - } else { - s_logger.info("Did not expunge " + vm); - return false; - } - } catch (OperationTimedoutException e) { - throw new CloudRuntimeException("Operation timed out", e); - } catch (ConcurrentOperationException e) { - throw new CloudRuntimeException("Concurrent operation ", e); - } - } - - @Override - public boolean advanceExpunge(T vm, User caller, Account account) throws ResourceUnavailableException, OperationTimedoutException, ConcurrentOperationException { - if (vm == null || vm.getRemoved() != null) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Unable to find vm or vm is destroyed: " + vm); - } - return true; - } - - if (!this.advanceStop(vm, false, caller, account)) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Unable to stop the VM so we can't expunge it."); - } - } - - try { - if (!stateTransitTo(vm, VirtualMachine.Event.ExpungeOperation, vm.getHostId())) { - s_logger.debug("Unable to destroy the vm because it is not in the correct state: " + vm); - return false; - } - } catch (NoTransitionException e) { - s_logger.debug("Unable to destroy the vm because it is not in the correct state: " + vm); - return false; - } - - if (s_logger.isDebugEnabled()) { - s_logger.debug("Destroying vm " + vm); - } - - VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm); - s_logger.debug("Cleaning up NICS"); - _networkMgr.cleanupNics(profile); - // Clean up volumes based on the vm's instance id - _storageMgr.cleanupVolumes(vm.getId()); - - VirtualMachineGuru guru = getVmGuru(vm); - guru.finalizeExpunge(vm); - - if (s_logger.isDebugEnabled()) { - s_logger.debug("Expunged " + vm); - } - - return true; - } - - @Override - public boolean start() { - _executor.scheduleAtFixedRate(new CleanupTask(), _cleanupInterval, _cleanupInterval, TimeUnit.SECONDS); - cancelWorkItems(_nodeId); - return true; - } - - @Override - public boolean stop() { - return true; - } - - @Override - public boolean configure(String name, Map xmlParams) throws ConfigurationException { - _name = name; - - ComponentLocator locator = ComponentLocator.getCurrentLocator(); - ConfigurationDao configDao = locator.getDao(ConfigurationDao.class); - Map params = configDao.getConfiguration(xmlParams); - - _retry = NumbersUtil.parseInt(params.get(Config.StartRetry.key()), 10); - - ReservationContextImpl.setComponents(_userDao, _domainDao, _accountDao); - VirtualMachineProfileImpl.setComponents(_offeringDao, _templateDao, _accountDao); - - _cancelWait = NumbersUtil.parseLong(params.get(Config.VmOpCancelInterval.key()), 3600); - _cleanupWait = NumbersUtil.parseLong(params.get(Config.VmOpCleanupWait.key()), 3600); - _cleanupInterval = NumbersUtil.parseLong(params.get(Config.VmOpCleanupInterval.key()), 86400) * 1000; - _opWaitInterval = NumbersUtil.parseLong(params.get(Config.VmOpWaitInterval.key()), 120) * 1000; - _lockStateRetry = NumbersUtil.parseInt(params.get(Config.VmOpLockStateRetry.key()), 5); - _operationTimeout = NumbersUtil.parseInt(params.get(Config.Wait.key()), 1800) * 2; - _forceStop = Boolean.parseBoolean(params.get(Config.VmDestroyForcestop.key())); - - _executor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("Vm-Operations-Cleanup")); - _nodeId = _clusterMgr.getManagementNodeId(); - - _agentMgr.registerForHostEvents(this, true, true, true); - - return true; - } - - @Override - public String getName() { - return _name; - } - - protected VirtualMachineManagerImpl() { - setStateMachine(); - } - - @Override - public T start(T vm, Map params, User caller, Account account) throws InsufficientCapacityException, ResourceUnavailableException { - return start(vm, params, caller, account, null); - } - - @Override - public T start(T vm, Map params, User caller, Account account, DeploymentPlan planToDeploy) throws InsufficientCapacityException, - ResourceUnavailableException { - try { - return advanceStart(vm, params, caller, account, planToDeploy); - } catch (ConcurrentOperationException e) { - throw new CloudRuntimeException("Unable to start a VM due to concurrent operation", e); - } - } - - protected boolean checkWorkItems(VMInstanceVO vm, State state) throws ConcurrentOperationException { - while (true) { - ItWorkVO vo = _workDao.findByOutstandingWork(vm.getId(), state); - if (vo == null) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Unable to find work for VM: " + vm + " and state: " + state); - } - return true; - } - - if (vo.getStep() == Step.Done) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Work for " + vm + " is " + vo.getStep()); - } - return true; - } - - if (vo.getSecondsTaskIsInactive() > _cancelWait) { - s_logger.warn("The task item for vm " + vm + " has been inactive for " + vo.getSecondsTaskIsInactive()); - return false; - } - - try { - Thread.sleep(_opWaitInterval); - } catch (InterruptedException e) { - s_logger.info("Waiting for " + vm + " but is interrupted"); - throw new ConcurrentOperationException("Waiting for " + vm + " but is interrupted"); - } - s_logger.debug("Waiting some more to make sure there's no activity on " + vm); - } - - } - - @DB - protected Ternary changeToStartState(VirtualMachineGuru vmGuru, T vm, User caller, Account account) - throws ConcurrentOperationException { - long vmId = vm.getId(); - - ItWorkVO work = new ItWorkVO(UUID.randomUUID().toString(), _nodeId, State.Starting, vm.getType(), vm.getId()); - int retry = _lockStateRetry; - while (retry-- != 0) { - Transaction txn = Transaction.currentTxn(); - Ternary result = null; - txn.start(); - try { - Journal journal = new Journal.LogJournal("Creating " + vm, s_logger); - work = _workDao.persist(work); - ReservationContextImpl context = new ReservationContextImpl(work.getId(), journal, caller, account); - - if (stateTransitTo(vm, Event.StartRequested, null, work.getId())) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Successfully transitioned to start state for " + vm + " reservation id = " + work.getId()); - } - result = new Ternary(vmGuru.findById(vmId), context, work); - txn.commit(); - return result; - } - } catch (NoTransitionException e) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Unable to transition into Starting state due to " + e.getMessage()); - } - } finally { - if (result == null) { - txn.rollback(); - } - } - - VMInstanceVO instance = _vmDao.findById(vmId); - if (instance == null) { - throw new ConcurrentOperationException("Unable to acquire lock on " + vm); - } - - if (s_logger.isDebugEnabled()) { - s_logger.debug("Determining why we're unable to update the state to Starting for " + instance + ". Retry=" + retry); - } - - State state = instance.getState(); - if (state == State.Running) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("VM is already started: " + vm); - } - return null; - } - - if (state.isTransitional()) { - if (!checkWorkItems(vm, state)) { - throw new ConcurrentOperationException("There are concurrent operations on " + vm); - } else { - continue; - } - } - - if (state != State.Stopped) { - s_logger.debug("VM " + vm + " is not in a state to be started: " + state); - return null; - } - } - - throw new ConcurrentOperationException("Unable to change the state of " + vm); - } - - protected boolean changeState(T vm, Event event, Long hostId, ItWorkVO work, Step step) throws NoTransitionException { - // FIXME: We should do this better. - Step previousStep = work.getStep(); - _workDao.updateStep(work, step); - boolean result = false; - try { - result = stateTransitTo(vm, event, hostId); - return result; - } finally { - if (!result) { - _workDao.updateStep(work, previousStep); - } - } - } - - @Override - public T advanceStart(T vm, Map params, User caller, Account account) throws InsufficientCapacityException, - ConcurrentOperationException, ResourceUnavailableException { - return advanceStart(vm, params, caller, account, null); - } - - @Override - public T advanceStart(T vm, Map params, User caller, Account account, DeploymentPlan planToDeploy) - throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException { - long vmId = vm.getId(); - VirtualMachineGuru vmGuru; - if (vm.getHypervisorType() == HypervisorType.BareMetal) { - vmGuru = getBareMetalVmGuru(vm); - } else { - vmGuru = getVmGuru(vm); - } - - vm = vmGuru.findById(vm.getId()); - Ternary start = changeToStartState(vmGuru, vm, caller, account); - if (start == null) { - return vmGuru.findById(vmId); - } - - vm = start.first(); - ReservationContext ctx = start.second(); - ItWorkVO work = start.third(); - - T startedVm = null; - ServiceOfferingVO offering = _offeringDao.findById(vm.getServiceOfferingId()); - VMTemplateVO template = _templateDao.findById(vm.getTemplateId()); - - if (s_logger.isDebugEnabled()) { - s_logger.debug("Trying to deploy VM, vm has dcId: " + vm.getDataCenterIdToDeployIn() + " and podId: " + vm.getPodIdToDeployIn()); - } - DataCenterDeployment plan = new DataCenterDeployment(vm.getDataCenterIdToDeployIn(), vm.getPodIdToDeployIn(), null, null, null, null, ctx); - if(planToDeploy != null && planToDeploy.getDataCenterId() != 0){ - if (s_logger.isDebugEnabled()) { - s_logger.debug("advanceStart: DeploymentPlan is provided, using dcId:" + planToDeploy.getDataCenterId() + ", podId: " + planToDeploy.getPodId() + ", clusterId: " - + planToDeploy.getClusterId() + ", hostId: " + planToDeploy.getHostId() + ", poolId: " + planToDeploy.getPoolId()); - } - plan = new DataCenterDeployment(planToDeploy.getDataCenterId(), planToDeploy.getPodId(), planToDeploy.getClusterId(), planToDeploy.getHostId(), planToDeploy.getPoolId(), planToDeploy.getPhysicalNetworkId(), ctx); - } - - HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vm.getHypervisorType()); - - boolean canRetry = true; - try { - Journal journal = start.second().getJournal(); - - ExcludeList avoids = null; - if (planToDeploy != null) { - avoids = planToDeploy.getAvoids(); - } - if (avoids == null) { - avoids = new ExcludeList(); - } - if (s_logger.isDebugEnabled()) { - s_logger.debug("Deploy avoids pods: " + avoids.getPodsToAvoid() + ", clusters: " + avoids.getClustersToAvoid() + ", hosts: " + avoids.getHostsToAvoid()); - } - - - boolean planChangedByVolume = false; - boolean reuseVolume = true; - DataCenterDeployment originalPlan = plan; - - int retry = _retry; - boolean recreate = false; - while (retry-- != 0) { // It's != so that it can match -1. - - if(reuseVolume){ - // edit plan if this vm's ROOT volume is in READY state already - List vols = _volsDao.findReadyRootVolumesByInstance(vm.getId()); - for (VolumeVO vol : vols) { - // make sure if the templateId is unchanged. If it is changed, - // let planner - // reassign pool for the volume even if it ready. - Long volTemplateId = vol.getTemplateId(); - if (volTemplateId != null && volTemplateId.longValue() != template.getId()) { - if (s_logger.isDebugEnabled()) { - s_logger.debug(vol + " of " + vm + " is READY, but template ids don't match, let the planner reassign a new pool"); - } - continue; - } - - StoragePoolVO pool = _storagePoolDao.findById(vol.getPoolId()); - if (!pool.isInMaintenance()) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Root volume is ready, need to place VM in volume's cluster"); - } - long rootVolDcId = pool.getDataCenterId(); - Long rootVolPodId = pool.getPodId(); - Long rootVolClusterId = pool.getClusterId(); - if (planToDeploy != null && planToDeploy.getDataCenterId() != 0) { - Long clusterIdSpecified = planToDeploy.getClusterId(); - if (clusterIdSpecified != null && rootVolClusterId != null) { - if (rootVolClusterId.longValue() != clusterIdSpecified.longValue()) { - // cannot satisfy the plan passed in to the - // planner - if (s_logger.isDebugEnabled()) { - s_logger.debug("Cannot satisfy the deployment plan passed in since the ready Root volume is in different cluster. volume's cluster: " + rootVolClusterId - + ", cluster specified: " + clusterIdSpecified); - } - throw new ResourceUnavailableException("Root volume is ready in different cluster, Deployment plan provided cannot be satisfied, unable to create a deployment for " - + vm, Cluster.class, clusterIdSpecified); - } - } - plan = new DataCenterDeployment(planToDeploy.getDataCenterId(), planToDeploy.getPodId(), planToDeploy.getClusterId(), planToDeploy.getHostId(), vol.getPoolId(), null, ctx); - }else{ - plan = new DataCenterDeployment(rootVolDcId, rootVolPodId, rootVolClusterId, null, vol.getPoolId(), null, ctx); - if (s_logger.isDebugEnabled()) { - s_logger.debug(vol + " is READY, changing deployment plan to use this pool's dcId: " + rootVolDcId + " , podId: " + rootVolPodId + " , and clusterId: " + rootVolClusterId); - } - planChangedByVolume = true; - } - } - } - } - - VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vm, template, offering, account, params); - DeployDestination dest = null; - for (DeploymentPlanner planner : _planners) { - if (planner.canHandle(vmProfile, plan, avoids)) { - dest = planner.plan(vmProfile, plan, avoids); - } else { - continue; - } - if (dest != null) { - avoids.addHost(dest.getHost().getId()); - journal.record("Deployment found ", vmProfile, dest); - break; - } - } - - if (dest == null) { - if (planChangedByVolume) { - plan = originalPlan; - planChangedByVolume = false; - //do not enter volume reuse for next retry, since we want to look for resorces outside the volume's cluster - reuseVolume = false; - continue; - } - throw new InsufficientServerCapacityException("Unable to create a deployment for " + vmProfile, DataCenter.class, plan.getDataCenterId()); - } - - long destHostId = dest.getHost().getId(); - vm.setPodId(dest.getPod().getId()); - - try { - if (!changeState(vm, Event.OperationRetry, destHostId, work, Step.Prepare)) { - throw new ConcurrentOperationException("Unable to update the state of the Virtual Machine"); - } - } catch (NoTransitionException e1) { - throw new ConcurrentOperationException(e1.getMessage()); - } - - try { - if (s_logger.isDebugEnabled()) { - s_logger.debug("VM is being created in podId: " + vm.getPodIdToDeployIn()); - } - _networkMgr.prepare(vmProfile, dest, ctx); - if (vm.getHypervisorType() != HypervisorType.BareMetal) { - _storageMgr.prepare(vmProfile, dest, recreate); - recreate = false; - } - //since StorageMgr succeeded in volume creation, reuse Volume for further tries until current cluster has capacity - if(!reuseVolume){ - reuseVolume = true; - } - - Commands cmds = null; - vmGuru.finalizeVirtualMachineProfile(vmProfile, dest, ctx); - - VirtualMachineTO vmTO = hvGuru.implement(vmProfile); - - cmds = new Commands(OnError.Stop); - cmds.addCommand(new StartCommand(vmTO)); - - vmGuru.finalizeDeployment(cmds, vmProfile, dest, ctx); - - - work = _workDao.findById(work.getId()); - if (work == null || work.getStep() != Step.Prepare) { - throw new ConcurrentOperationException("Work steps have been changed: " + work); - } - _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(); - if( host_guid != null ) { - HostVO finalHost = _resourceMgr.findHostByGuid(host_guid); - if (finalHost == null ) { - throw new CloudRuntimeException("Host Guid " + host_guid + " doesn't exist in DB, something wrong here"); - } - destHostId = finalHost.getId(); - } - if (vmGuru.finalizeStart(vmProfile, destHostId, cmds, ctx)) { - if (!changeState(vm, Event.OperationSucceeded, destHostId, work, Step.Done)) { - throw new ConcurrentOperationException("Unable to transition to a new state."); - } - startedVm = vm; - if (s_logger.isDebugEnabled()) { - s_logger.debug("Start completed for VM " + vm); - } - return startedVm; - } else { - 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()) { - s_logger.warn("Unable to stop " + vm + " due to " + (answer != null ? answer.getDetails() : "no answers")); - _haMgr.scheduleStop(vm, destHostId, WorkType.ForceStop); - 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; - } else { - 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()) { - _haMgr.scheduleStop(vm, destHostId, WorkType.CheckStop); - } - canRetry = false; - throw new AgentUnavailableException("Unable to start " + vm.getHostName(), destHostId, e); - } catch (ResourceUnavailableException e) { - s_logger.info("Unable to contact resource.", e); - if (!avoids.add(e)) { - if (e.getScope() == Volume.class || e.getScope() == Nic.class) { - throw e; - } else { - s_logger.warn("unexpected ResourceUnavailableException : " + e.getScope().getName(), e); - throw e; - } - } - } catch (InsufficientCapacityException e) { - s_logger.info("Insufficient capacity ", e); - if (!avoids.add(e)) { - if (e.getScope() == Volume.class || e.getScope() == Nic.class) { - throw e; - } else { - s_logger.warn("unexpected InsufficientCapacityException : " + e.getScope().getName(), e); - } - } - } catch (Exception e) { - s_logger.error("Failed to start instance " + vm, e); - throw new AgentUnavailableException("Unable to start instance due to " + e.getMessage(), destHostId, e); - } finally { - if (startedVm == null && canRetry) { - Step prevStep = work.getStep(); - _workDao.updateStep(work, Step.Release); - if (prevStep == Step.Started || prevStep == Step.Starting) { - cleanup(vmGuru, vmProfile, work, Event.OperationFailed, false, caller, account); - } else { - //if step is not starting/started, send cleanup command with force=true - cleanup(vmGuru, vmProfile, work, Event.OperationFailed, true, caller, account); - } - } - } - } - } finally { - if (startedVm == null) { - if (canRetry) { - try { - changeState(vm, Event.OperationFailed, null, work, Step.Done); - } catch (NoTransitionException e) { - throw new ConcurrentOperationException(e.getMessage()); - } - } - } - } - - return startedVm; - } - - @Override - public boolean stop(T vm, User user, Account account) throws ResourceUnavailableException { - try { - return advanceStop(vm, false, user, account); - } catch (OperationTimedoutException e) { - throw new AgentUnavailableException("Unable to stop vm because the operation to stop timed out", vm.getHostId(), e); - } catch (ConcurrentOperationException e) { - throw new CloudRuntimeException("Unable to stop vm because of a concurrent operation", e); - } - } - - protected boolean sendStop(VirtualMachineGuru guru, VirtualMachineProfile profile, boolean force) { - VMInstanceVO vm = profile.getVirtualMachine(); - StopCommand stop = new StopCommand(vm, vm.getInstanceName(), null); - try { - Answer answer = _agentMgr.send(vm.getHostId(), stop); - if (!answer.getResult()) { - s_logger.debug("Unable to stop VM due to " + answer.getDetails()); - return false; - } - - guru.finalizeStop(profile, (StopAnswer) answer); - } catch (AgentUnavailableException e) { - if (!force) { - return false; - } - } catch (OperationTimedoutException e) { - if (!force) { - return false; - } - } - - return true; - } - - protected boolean cleanup(VirtualMachineGuru guru, VirtualMachineProfile profile, ItWorkVO work, Event event, boolean force, User user, Account account) { - T vm = profile.getVirtualMachine(); - State state = vm.getState(); - s_logger.debug("Cleaning up resources for the vm " + vm + " in " + state + " state"); - if (state == State.Starting) { - Step step = work.getStep(); - if (step == Step.Starting && !force) { - s_logger.warn("Unable to cleanup vm " + vm + "; work state is incorrect: " + step); - return false; - } - - if (step == Step.Started || step == Step.Starting || step == Step.Release) { - if (vm.getHostId() != null) { - if (!sendStop(guru, profile, force)) { - s_logger.warn("Failed to stop vm " + vm + " in " + State.Starting + " state as a part of cleanup process"); - return false; - } - } - } - - if (step != Step.Release && step != Step.Prepare && step != Step.Started && step != Step.Starting) { - s_logger.debug("Cleanup is not needed for vm " + vm + "; work state is incorrect: " + step); - return true; - } - } else if (state == State.Stopping) { - if (vm.getHostId() != null) { - if (!sendStop(guru, profile, force)) { - s_logger.warn("Failed to stop vm " + vm + " in " + State.Stopping + " state as a part of cleanup process"); - return false; - } - } - } else if (state == State.Migrating) { - if (vm.getHostId() != null) { - if (!sendStop(guru, profile, force)) { - s_logger.warn("Failed to stop vm " + vm + " in " + State.Migrating + " state as a part of cleanup process"); - return false; - } - } - if (vm.getLastHostId() != null) { - if (!sendStop(guru, profile, force)) { - s_logger.warn("Failed to stop vm " + vm + " in " + State.Migrating + " state as a part of cleanup process"); - return false; - } - } - } else if (state == State.Running) { - if (!sendStop(guru, profile, force)) { - s_logger.warn("Failed to stop vm " + vm + " in " + State.Running + " state as a part of cleanup process"); - return false; - } - } - - try { - _networkMgr.release(profile, force); - s_logger.debug("Successfully released network resources for the vm " + vm); - } catch (Exception e) { - s_logger.warn("Unable to release some network resources.", e); - } - - _storageMgr.release(profile); - s_logger.debug("Successfully cleanued up resources for the vm " + vm + " in " + state + " state"); - return true; - } - - @Override - public boolean advanceStop(T vm, boolean forced, User user, Account account) throws AgentUnavailableException, OperationTimedoutException, ConcurrentOperationException { - State state = vm.getState(); - if (state == State.Stopped) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("VM is already stopped: " + vm); - } - return true; - } - - if (state == State.Destroyed || state == State.Expunging || state == State.Error) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Stopped called on " + vm + " but the state is " + state); - } - return true; - } - // grab outstanding work item if any - ItWorkVO work = _workDao.findByOutstandingWork(vm.getId(), vm.getState()); - if (work != null) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Found an outstanding work item for this vm " + vm + " with state:" + vm.getState() + ", work id:" + work.getId()); - } - } - Long hostId = vm.getHostId(); - if (hostId == null) { - if (!forced) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("HostId is null but this is not a forced stop, cannot stop vm " + vm + " with state:" + vm.getState()); - } - return false; - } - try { - stateTransitTo(vm, Event.AgentReportStopped, null, null); - } catch (NoTransitionException e) { - s_logger.warn(e.getMessage()); - } - // mark outstanding work item if any as done - if (work != null) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Updating work item to Done, id:" + work.getId()); - } - work.setStep(Step.Done); - _workDao.update(work.getId(), work); - } - return true; - } - - VirtualMachineGuru vmGuru = getVmGuru(vm); - VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm); - - try { - if (!stateTransitTo(vm, Event.StopRequested, vm.getHostId())) { - throw new ConcurrentOperationException("VM is being operated on."); - } - } catch (NoTransitionException e1) { - if (!forced) { - throw new CloudRuntimeException("We cannot stop " + vm + " when it is in state " + vm.getState()); - } - boolean doCleanup = false; - if (s_logger.isDebugEnabled()) { - s_logger.debug("Unable to transition the state but we're moving on because it's forced stop"); - } - if (state == State.Starting || state == State.Migrating) { - if (work != null) { - doCleanup = true; - } else { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Unable to cleanup VM: " + vm + " ,since outstanding work item is not found"); - } - throw new CloudRuntimeException("Work item not found, We cannot stop " + vm + " when it is in state " + vm.getState()); - } - } else if (state == State.Stopping) { - doCleanup = true; - } - - if (doCleanup) { - if (cleanup(vmGuru, new VirtualMachineProfileImpl(vm), work, Event.StopRequested, forced, user, account)) { - try { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Updating work item to Done, id:" + work.getId()); - } - return changeState(vm, Event.AgentReportStopped, null, work, Step.Done); - } catch (NoTransitionException e) { - s_logger.warn("Unable to cleanup " + vm); - return false; - } - } else { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Failed to cleanup VM: " + vm); - } - throw new CloudRuntimeException("Failed to cleanup " + vm + " , current state " + vm.getState()); - } - } - } - - if (vm.getState() != State.Stopping) { - throw new CloudRuntimeException("We cannot proceed with stop VM " + vm + " since it is not in 'Stopping' state, current state: " + vm.getState()); - } - - vmGuru.prepareStop(profile); - - StopCommand stop = new StopCommand(vm, vm.getInstanceName(), null); - boolean stopped = false; - StopAnswer answer = null; - try { - answer = (StopAnswer) _agentMgr.send(vm.getHostId(), stop); - stopped = answer.getResult(); - if (!stopped) { - throw new CloudRuntimeException("Unable to stop the virtual machine due to " + answer.getDetails()); - } - vmGuru.finalizeStop(profile, answer); - - } catch (AgentUnavailableException e) { - } catch (OperationTimedoutException e) { - } finally { - if (!stopped) { - if (!forced) { - s_logger.warn("Unable to stop vm " + vm); - try { - stateTransitTo(vm, Event.OperationFailed, vm.getHostId()); - } catch (NoTransitionException e) { - s_logger.warn("Unable to transition the state " + vm); - } - return false; - } else { - s_logger.warn("Unable to actually stop " + vm + " but continue with release because it's a force stop"); - vmGuru.finalizeStop(profile, answer); - } - } - } - - if (s_logger.isDebugEnabled()) { - s_logger.debug(vm + " is stopped on the host. Proceeding to release resource held."); - } - - try { - _networkMgr.release(profile, forced); - s_logger.debug("Successfully released network resources for the vm " + vm); - } catch (Exception e) { - s_logger.warn("Unable to release some network resources.", e); - } - - try { - if (vm.getHypervisorType() != HypervisorType.BareMetal) { - _storageMgr.release(profile); - s_logger.debug("Successfully released storage resources for the vm " + vm); - } - } catch (Exception e) { - s_logger.warn("Unable to release storage resources.", e); - } - - try { - if (work != null) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Updating the outstanding work item to Done, id:" + work.getId()); - } - work.setStep(Step.Done); - _workDao.update(work.getId(), work); - } - - return stateTransitTo(vm, Event.OperationSucceeded, null, null); - } catch (NoTransitionException e) { - s_logger.warn(e.getMessage()); - return false; - } - } - - private void setStateMachine() { - _stateMachine = VirtualMachine.State.getStateMachine(); - } - - protected boolean stateTransitTo(VMInstanceVO vm, VirtualMachine.Event e, Long hostId, String reservationId) throws NoTransitionException { - vm.setReservationId(reservationId); - return _stateMachine.transitTo(vm, e, new Pair(vm.getHostId(), hostId), _vmDao); - } - - @Override - public boolean stateTransitTo(VMInstanceVO vm, VirtualMachine.Event e, Long hostId) throws NoTransitionException { - State oldState = vm.getState(); - if (oldState == State.Starting) { - if (e == Event.OperationSucceeded) { - vm.setLastHostId(hostId); - } - } else if (oldState == State.Stopping) { - if (e == Event.OperationSucceeded) { - vm.setLastHostId(vm.getHostId()); - } - } - return _stateMachine.transitTo(vm, e, new Pair(vm.getHostId(), hostId), _vmDao); - } - - @Override - public boolean remove(T vm, User user, Account caller) { - return _vmDao.remove(vm.getId()); - } - - @Override - public boolean destroy(T vm, User user, Account caller) throws AgentUnavailableException, OperationTimedoutException, ConcurrentOperationException { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Destroying vm " + vm); - } - if (vm == null || vm.getState() == State.Destroyed || vm.getState() == State.Expunging || vm.getRemoved() != null) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Unable to find vm or vm is destroyed: " + vm); - } - return true; - } - - if (!advanceStop(vm, _forceStop, user, caller)) { - s_logger.debug("Unable to stop " + vm); - return false; - } - - try { - if (!stateTransitTo(vm, VirtualMachine.Event.DestroyRequested, vm.getHostId())) { - s_logger.debug("Unable to destroy the vm because it is not in the correct state: " + vm); - return false; - } - } catch (NoTransitionException e) { - s_logger.debug(e.getMessage()); - return false; - } - - return true; - } - - protected boolean checkVmOnHost(VirtualMachine vm, long hostId) throws AgentUnavailableException, OperationTimedoutException { - CheckVirtualMachineAnswer answer = (CheckVirtualMachineAnswer) _agentMgr.send(hostId, new CheckVirtualMachineCommand(vm.getInstanceName())); - if (!answer.getResult() || answer.getState() == State.Stopped) { - return false; - } - - return true; - } - - @Override - public T storageMigration(T vm, StoragePool destPool) { - VirtualMachineGuru vmGuru = getVmGuru(vm); - - long vmId = vm.getId(); - vm = vmGuru.findById(vmId); - - try { - stateTransitTo(vm, VirtualMachine.Event.StorageMigrationRequested, null); - } catch (NoTransitionException e) { - s_logger.debug("Unable to migrate vm: " + e.toString()); - throw new CloudRuntimeException("Unable to migrate vm: " + e.toString()); - } - - VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm); - boolean migrationResult = false; - try { - migrationResult = _storageMgr.StorageMigration(profile, destPool); - - if (migrationResult) { - //if the vm is migrated to different pod in basic mode, need to reallocate ip - - if (vm.getPodIdToDeployIn() != destPool.getPodId()) { - DataCenterDeployment plan = new DataCenterDeployment(vm.getDataCenterIdToDeployIn(), destPool.getPodId(), null, null, null, null); - VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vm, null, null, null, null); - _networkMgr.reallocate(vmProfile, plan); - } - - //when start the vm next time, don;'t look at last_host_id, only choose the host based on volume/storage pool - vm.setLastHostId(null); - vm.setPodId(destPool.getPodId()); - } else { - s_logger.debug("Storage migration failed"); - } - } catch (ConcurrentOperationException e) { - s_logger.debug("Failed to migration: " + e.toString()); - throw new CloudRuntimeException("Failed to migration: " + e.toString()); - } catch (InsufficientVirtualNetworkCapcityException e) { - s_logger.debug("Failed to migration: " + e.toString()); - throw new CloudRuntimeException("Failed to migration: " + e.toString()); - } catch (InsufficientAddressCapacityException e) { - s_logger.debug("Failed to migration: " + e.toString()); - throw new CloudRuntimeException("Failed to migration: " + e.toString()); - } catch (InsufficientCapacityException e) { - s_logger.debug("Failed to migration: " + e.toString()); - throw new CloudRuntimeException("Failed to migration: " + e.toString()); - } finally { - try { - stateTransitTo(vm, VirtualMachine.Event.AgentReportStopped, null); - } catch (NoTransitionException e) { - s_logger.debug("Failed to change vm state: " + e.toString()); - throw new CloudRuntimeException("Failed to change vm state: " + e.toString()); - } - } - - return vm; - } - - @Override - public T migrate(T vm, long srcHostId, DeployDestination dest) throws ResourceUnavailableException, ConcurrentOperationException, ManagementServerException, - VirtualMachineMigrationException { - s_logger.info("Migrating " + vm + " to " + dest); - - long dstHostId = dest.getHost().getId(); - Host fromHost = _hostDao.findById(srcHostId); - if (fromHost == null) { - s_logger.info("Unable to find the host to migrate from: " + srcHostId); - throw new CloudRuntimeException("Unable to find the host to migrate from: " + srcHostId); - } - - if (fromHost.getClusterId().longValue() != dest.getCluster().getId()) { - s_logger.info("Source and destination host are not in same cluster, unable to migrate to host: " + dest.getHost().getId()); - throw new CloudRuntimeException("Source and destination host are not in same cluster, unable to migrate to host: " + dest.getHost().getId()); - } - - VirtualMachineGuru vmGuru = getVmGuru(vm); - - long vmId = vm.getId(); - vm = vmGuru.findById(vmId); - if (vm == null) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Unable to find the vm " + vm); - } - throw new ManagementServerException("Unable to find a virtual machine with id " + vmId); - } - - if (vm.getState() != State.Running) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("VM is not Running, unable to migrate the vm " + vm); - } - throw new VirtualMachineMigrationException("VM is not Running, unable to migrate the vm currently " + vm + " , current state: " + vm.getState().toString()); - } - - short alertType = AlertManager.ALERT_TYPE_USERVM_MIGRATE; - if (VirtualMachine.Type.DomainRouter.equals(vm.getType())) { - alertType = AlertManager.ALERT_TYPE_DOMAIN_ROUTER_MIGRATE; - } else if (VirtualMachine.Type.ConsoleProxy.equals(vm.getType())) { - alertType = AlertManager.ALERT_TYPE_CONSOLE_PROXY_MIGRATE; - } - - VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm); - _networkMgr.prepareNicForMigration(profile, dest); - _storageMgr.prepareForMigration(profile, dest); - - VirtualMachineTO to = toVmTO(profile); - PrepareForMigrationCommand pfmc = new PrepareForMigrationCommand(to); - - ItWorkVO work = new ItWorkVO(UUID.randomUUID().toString(), _nodeId, State.Migrating, vm.getType(), vm.getId()); - work.setStep(Step.Prepare); - work.setResourceType(ItWorkVO.ResourceType.Host); - work.setResourceId(dstHostId); - work = _workDao.persist(work); - - PrepareForMigrationAnswer pfma = null; - try { - pfma = (PrepareForMigrationAnswer) _agentMgr.send(dstHostId, pfmc); - if (!pfma.getResult()) { - String msg = "Unable to prepare for migration due to " + pfma.getDetails(); - pfma = null; - throw new AgentUnavailableException(msg, dstHostId); - } - } catch (OperationTimedoutException e1) { - throw new AgentUnavailableException("Operation timed out", dstHostId); - } finally { - if (pfma == null) { - work.setStep(Step.Done); - _workDao.update(work.getId(), work); - } - } - - vm.setLastHostId(srcHostId); - try { - if (vm == null || vm.getHostId() == null || vm.getHostId() != srcHostId || !changeState(vm, Event.MigrationRequested, dstHostId, work, Step.Migrating)) { - s_logger.info("Migration cancelled because state has changed: " + vm); - throw new ConcurrentOperationException("Migration cancelled because state has changed: " + vm); - } - } catch (NoTransitionException e1) { - s_logger.info("Migration cancelled because " + e1.getMessage()); - throw new ConcurrentOperationException("Migration cancelled because " + e1.getMessage()); - } - - boolean migrated = false; - try { - boolean isWindows = _guestOsCategoryDao.findById(_guestOsDao.findById(vm.getGuestOSId()).getCategoryId()).getName().equalsIgnoreCase("Windows"); - MigrateCommand mc = new MigrateCommand(vm.getInstanceName(), dest.getHost().getPrivateIpAddress(), isWindows); - mc.setHostGuid(dest.getHost().getGuid()); - - try { - MigrateAnswer ma = (MigrateAnswer) _agentMgr.send(vm.getLastHostId(), mc); - if (!ma.getResult()) { - s_logger.error("Unable to migrate due to " + ma.getDetails()); - return null; - } - } catch (OperationTimedoutException e) { - if (e.isActive()) { - s_logger.warn("Active migration command so scheduling a restart for " + vm); - _haMgr.scheduleRestart(vm, true); - } - throw new AgentUnavailableException("Operation timed out on migrating " + vm, dstHostId); - } - - try { - if (!changeState(vm, VirtualMachine.Event.OperationSucceeded, dstHostId, work, Step.Started)) { - throw new ConcurrentOperationException("Unable to change the state for " + vm); - } - } catch (NoTransitionException e1) { - throw new ConcurrentOperationException("Unable to change state due to " + e1.getMessage()); - } - - try { - if (!checkVmOnHost(vm, dstHostId)) { - s_logger.error("Unable to complete migration for " + vm); - try { - _agentMgr.send(srcHostId, new Commands(cleanup(vm.getInstanceName())), null); - } catch (AgentUnavailableException e) { - s_logger.error("AgentUnavailableException while cleanup on source host: " + srcHostId); - } - cleanup(vmGuru, new VirtualMachineProfileImpl(vm), work, Event.AgentReportStopped, true, _accountMgr.getSystemUser(), _accountMgr.getSystemAccount()); - return null; - } - } catch (OperationTimedoutException e) { - } - - migrated = true; - return vm; - } finally { - if (!migrated) { - s_logger.info("Migration was unsuccessful. Cleaning up: " + vm); - - _alertMgr.sendAlert(alertType, fromHost.getDataCenterId(), fromHost.getPodId(), "Unable to migrate vm " + vm.getInstanceName() + " from host " + fromHost.getName() + " in zone " - + dest.getDataCenter().getName() + " and pod " + dest.getPod().getName(), "Migrate Command failed. Please check logs."); - try { - _agentMgr.send(dstHostId, new Commands(cleanup(vm.getInstanceName())), null); - } catch (AgentUnavailableException ae) { - s_logger.info("Looks like the destination Host is unavailable for cleanup"); - } - - try { - stateTransitTo(vm, Event.OperationFailed, srcHostId); - } catch (NoTransitionException e) { - s_logger.warn(e.getMessage()); - } - } - - work.setStep(Step.Done); - _workDao.update(work.getId(), work); - } - } - - @Override - public VirtualMachineTO toVmTO(VirtualMachineProfile profile) { - HypervisorGuru hvGuru = _hvGuruMgr.getGuru(profile.getVirtualMachine().getHypervisorType()); - VirtualMachineTO to = hvGuru.implement(profile); - return to; - } - - protected void cancelWorkItems(long nodeId) { - GlobalLock scanLock = GlobalLock.getInternLock("vmmgr.cancel.workitem"); - - try { - if (scanLock.lock(3)) { - try { - List works = _workDao.listWorkInProgressFor(nodeId); - for (ItWorkVO work : works) { - s_logger.info("Handling unfinished work item: " + work); - try { - VMInstanceVO vm = _vmDao.findById(work.getInstanceId()); - if (vm != null) { - if (work.getType() == State.Starting) { - _haMgr.scheduleRestart(vm, true); - work.setManagementServerId(_nodeId); - _workDao.update(work.getId(), work); - } else if (work.getType() == State.Stopping) { - _haMgr.scheduleStop(vm, vm.getHostId(), WorkType.CheckStop); - work.setManagementServerId(_nodeId); - _workDao.update(work.getId(), work); - } else if (work.getType() == State.Migrating) { - _haMgr.scheduleMigration(vm); - work.setStep(Step.Done); - _workDao.update(work.getId(), work); - } - } - } catch (Exception e) { - s_logger.error("Error while handling " + work, e); - } - } - } finally { - scanLock.unlock(); - } - } - } finally { - scanLock.releaseRef(); - } - } - - @Override - public boolean migrateAway(VirtualMachine.Type vmType, long vmId, long srcHostId) throws InsufficientServerCapacityException, VirtualMachineMigrationException { - VirtualMachineGuru vmGuru = _vmGurus.get(vmType); - VMInstanceVO vm = vmGuru.findById(vmId); - if (vm == null) { - s_logger.debug("Unable to find a VM for " + vmId); - return true; - } - - VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm); - - Long hostId = vm.getHostId(); - if (hostId == null) { - s_logger.debug("Unable to migrate because the VM doesn't have a host id: " + vm); - return true; - } - - Host host = _hostDao.findById(hostId); - - DataCenterDeployment plan = new DataCenterDeployment(host.getDataCenterId(), host.getPodId(), host.getClusterId(), null, null, null); - ExcludeList excludes = new ExcludeList(); - excludes.addHost(hostId); - - DeployDestination dest = null; - while (true) { - for (DeploymentPlanner planner : _planners) { - if (planner.canHandle(profile, plan, excludes)) { - dest = planner.plan(profile, plan, excludes); - } else { - continue; - } - - if (dest != null) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Planner " + planner + " found " + dest + " for migrating to."); - } - break; - } - if (s_logger.isDebugEnabled()) { - s_logger.debug("Planner " + planner + " was unable to find anything."); - } - } - - if (dest == null) { - throw new InsufficientServerCapacityException("Unable to find a server to migrate to.", host.getClusterId()); - } - - excludes.addHost(dest.getHost().getId()); - VMInstanceVO vmInstance = null; - try { - vmInstance = migrate(vm, srcHostId, dest); - } catch (ResourceUnavailableException e) { - s_logger.debug("Unable to migrate to unavailable " + dest); - } catch (ConcurrentOperationException e) { - s_logger.debug("Unable to migrate VM due to: " + e.getMessage()); - } catch (ManagementServerException e) { - s_logger.debug("Unable to migrate VM: " + e.getMessage()); - } catch (VirtualMachineMigrationException e) { - s_logger.debug("Got VirtualMachineMigrationException, Unable to migrate: " + e.getMessage()); - if (vm.getState() == State.Starting) { - s_logger.debug("VM seems to be still Starting, we should retry migration later"); - throw e; - } else { - s_logger.debug("Unable to migrate VM, VM is not in Running or even Starting state, current state: " + vm.getState().toString()); - } - } - if (vmInstance != null) { - return true; - } - try { - boolean result = advanceStop(vm, true, _accountMgr.getSystemUser(), _accountMgr.getSystemAccount()); - return result; - } catch (ResourceUnavailableException e) { - s_logger.debug("Unable to stop VM due to " + e.getMessage()); - } catch (ConcurrentOperationException e) { - s_logger.debug("Unable to stop VM due to " + e.getMessage()); - } catch (OperationTimedoutException e) { - s_logger.debug("Unable to stop VM due to " + e.getMessage()); - } - return false; - } - } - - protected class CleanupTask implements Runnable { - @Override - public void run() { - s_logger.trace("VM Operation Thread Running"); - try { - _workDao.cleanup(_cleanupWait); - } catch (Exception e) { - s_logger.error("VM Operations failed due to ", e); - } - } - } - - @Override - public boolean isVirtualMachineUpgradable(VirtualMachine vm, ServiceOffering offering) { - Enumeration en = _hostAllocators.enumeration(); - boolean isMachineUpgradable = true; - while (isMachineUpgradable && en.hasMoreElements()) { - final HostAllocator allocator = en.nextElement(); - isMachineUpgradable = allocator.isVirtualMachineUpgradable(vm, offering); - } - - return isMachineUpgradable; - } - - @Override - public T reboot(T vm, Map params, User caller, Account account) throws InsufficientCapacityException, ResourceUnavailableException { - try { - return advanceReboot(vm, params, caller, account); - } catch (ConcurrentOperationException e) { - throw new CloudRuntimeException("Unable to reboot a VM due to concurrent operation", e); - } - } - - @Override - public T advanceReboot(T vm, Map params, User caller, Account account) throws InsufficientCapacityException, - ConcurrentOperationException, ResourceUnavailableException { - T rebootedVm = null; - - DataCenter dc = _configMgr.getZone(vm.getDataCenterIdToDeployIn()); - Host host = _hostDao.findById(vm.getHostId()); - Cluster cluster = null; - if (host != null) { - cluster = _configMgr.getCluster(host.getClusterId()); - } - HostPodVO pod = _configMgr.getPod(host.getPodId()); - DeployDestination dest = new DeployDestination(dc, pod, cluster, host); - - try { - - Commands cmds = new Commands(OnError.Stop); - cmds.addCommand(new RebootCommand(vm.getInstanceName())); - _agentMgr.send(host.getId(), cmds); - - Answer rebootAnswer = cmds.getAnswer(RebootAnswer.class); - if (rebootAnswer != null && rebootAnswer.getResult()) { - rebootedVm = vm; - return rebootedVm; - } - s_logger.info("Unable to reboot VM " + vm + " on " + dest.getHost() + " due to " + (rebootAnswer == null ? " no reboot answer" : rebootAnswer.getDetails())); - } catch (OperationTimedoutException e) { - s_logger.warn("Unable to send the reboot command to host " + dest.getHost() + " for the vm " + vm + " due to operation timeout", e); - throw new CloudRuntimeException("Failed to reboot the vm on host " + dest.getHost()); - } - - return rebootedVm; - } - - @Override - public VMInstanceVO findByIdAndType(VirtualMachine.Type type, long vmId) { - VirtualMachineGuru guru = _vmGurus.get(type); - return guru.findById(vmId); - } - - public Command cleanup(String vmName) { - return new StopCommand(vmName); - } - - public Commands fullHostSync(final long hostId, StartupRoutingCommand startup) { - Commands commands = new Commands(OnError.Continue); - - Map infos = convertToInfos(startup); - - final List vms = _vmDao.listByHostId(hostId); - s_logger.debug("Found " + vms.size() + " VMs for host " + hostId); - for (VMInstanceVO vm : vms) { - AgentVmInfo info = infos.remove(vm.getId()); - VMInstanceVO castedVm = null; - if (info == null) { - info = new AgentVmInfo(vm.getInstanceName(), getVmGuru(vm), vm, State.Stopped); - } - castedVm = info.guru.findById(vm.getId()); - - HypervisorGuru hvGuru = _hvGuruMgr.getGuru(castedVm.getHypervisorType()); - Command command = compareState(hostId, castedVm, info, true, hvGuru.trackVmHostChange()); - if (command != null) { - commands.addCommand(command); - } - } - - for (final AgentVmInfo left : infos.values()) { - boolean found = false; - for (VirtualMachineGuru vmGuru : _vmGurus.values()) { - VMInstanceVO vm = vmGuru.findByName(left.name); - if (vm != null) { - found = true; - HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vm.getHypervisorType()); - if(hvGuru.trackVmHostChange()) { - Command command = compareState(hostId, vm, left, true, true); - if (command != null) { - commands.addCommand(command); - } - } else { - s_logger.warn("Stopping a VM, VM " + left.name + " migrate from Host " + vm.getHostId() + " to Host " + hostId ); - commands.addCommand(cleanup(left.name)); - } - break; - } - } - if ( ! found ) { - s_logger.warn("Stopping a VM that we have no record of : " + left.name); - commands.addCommand(cleanup(left.name)); - } - } - - return commands; - } - - public Commands deltaHostSync(long hostId, Map newStates) { - Map states = convertDeltaToInfos(newStates); - Commands commands = new Commands(OnError.Continue); - - for (Map.Entry entry : states.entrySet()) { - AgentVmInfo info = entry.getValue(); - - VMInstanceVO vm = info.vm; - - Command command = null; - if (vm != null) { - HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vm.getHypervisorType()); - command = compareState(hostId, vm, info, false, hvGuru.trackVmHostChange()); - } else { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Cleaning up a VM that is no longer found: " + info.name); - } - command = cleanup(info.name); - } - - if (command != null) { - commands.addCommand(command); - } - } - - return commands; - } - - - - public void deltaSync(Map> newStates) { - Map states = convertToInfos(newStates); - - for (Map.Entry entry : states.entrySet()) { - AgentVmInfo info = entry.getValue(); - VMInstanceVO vm = info.vm; - Command command = null; - if (vm != null) { - Host host = _resourceMgr.findHostByGuid(info.getHostUuid()); - long hId = host.getId(); - - HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vm.getHypervisorType()); - command = compareState(hId, vm, info, false, hvGuru.trackVmHostChange()); - } else { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Cleaning up a VM that is no longer found : " + info.name); - } - command = cleanup(info.name); - } - if (command != null){ - try { - Host host = _resourceMgr.findHostByGuid(info.getHostUuid()); - if (host != null){ - 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()); - } - } - } - } - - - public void fullSync(final long clusterId, Map> newStates) { - if (newStates==null)return; - Map infos = convertToInfos(newStates); - Set set_vms = Collections.synchronizedSet(new HashSet()); - set_vms.addAll(_vmDao.listByClusterId(clusterId)); - set_vms.addAll(_vmDao.listLHByClusterId(clusterId)); - - for (VMInstanceVO vm : set_vms) { - if (vm.isRemoved() || vm.getState() == State.Destroyed || vm.getState() == State.Expunging) continue; - 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))) - { - 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) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Found an outstanding work item for this vm " + vm + " in state:" + vm.getState() + ", work id:" + work.getId()); - } - } - vm.setState(State.Running); // set it as running and let HA take care of it - _vmDao.persist(vm); - - if (work != null) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Updating outstanding work item to Done, id:" + work.getId()); - } - work.setStep(Step.Done); - _workDao.update(work.getId(), work); - } - - castedVm = info.guru.findById(vm.getId()); - try { - Host host = _hostDao.findByGuid(info.getHostUuid()); - long hostId = host == null ? (vm.getHostId() == null ? vm.getLastHostId() : vm.getHostId()) : host.getId(); - HypervisorGuru hvGuru = _hvGuruMgr.getGuru(castedVm.getHypervisorType()); - Command command = compareState(hostId, castedVm, info, true, hvGuru.trackVmHostChange()); - if (command != null){ - Answer answer = _agentMgr.send(hostId, command); - if (!answer.getResult()) { - s_logger.warn("Failed to update state of the VM due to " + answer.getDetails()); - } - } - } catch (Exception e) { - s_logger.warn("Unable to update state of the VM due to exception " + e.getMessage()); - e.printStackTrace(); - } - } - else if (info != null && (vm.getState() == State.Stopped || vm.getState() == State.Stopping)) { - 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()); - } - } - } - 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 - 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()); - vm.setHostId(null); - _vmDao.persist(vm); - }*/ - } - - for (final AgentVmInfo left : infos.values()) { - 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){ - s_logger.warn("Stopping a VM which we do not have any record of " + left.name); - Answer answer = _agentMgr.send(host.getId(), cleanup(left.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()); - } - } - - } - - - - protected Map convertToInfos(final Map> newStates) { - final HashMap map = new HashMap(); - if (newStates == null) { - return map; - } - Collection> vmGurus = _vmGurus.values(); - boolean is_alien_vm = true; - long alien_vm_count = -1; - for (Map.Entry> entry : newStates.entrySet()) { - is_alien_vm = true; - for (VirtualMachineGuru vmGuru : vmGurus) { - String name = entry.getKey(); - VMInstanceVO vm = vmGuru.findByName(name); - if (vm != null) { - map.put(vm.getId(), new AgentVmInfo(entry.getKey(), vmGuru, vm, entry.getValue().second(), entry.getValue().first())); - is_alien_vm = false; - break; - } - Long id = vmGuru.convertToId(name); - if (id != null) { - map.put(id, new AgentVmInfo(entry.getKey(), vmGuru, null, entry.getValue().second(), entry.getValue().first())); - is_alien_vm = false; - break; - } - } - // 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()); - } - } - return map; - } - - protected Map convertToInfos(StartupRoutingCommand cmd) { - final Map states = cmd.getVmStates(); - final HashMap map = new HashMap(); - if (states == null) { - return map; - } - Collection> vmGurus = _vmGurus.values(); - - for (Map.Entry entry : states.entrySet()) { - for (VirtualMachineGuru vmGuru : vmGurus) { - String name = entry.getKey(); - VMInstanceVO vm = vmGuru.findByName(name); - if (vm != null) { - map.put(vm.getId(), new AgentVmInfo(entry.getKey(), vmGuru, vm, entry.getValue().getState(), entry.getValue().getHost() )); - break; - } - Long id = vmGuru.convertToId(name); - if (id != null) { - map.put(id, new AgentVmInfo(entry.getKey(), vmGuru, null,entry.getValue().getState(), entry.getValue().getHost() )); - break; - } - } - } - - return map; - } - - protected Map convertDeltaToInfos(final Map states) { - final HashMap map = new HashMap(); - - if (states == null) { - return map; - } - - Collection> vmGurus = _vmGurus.values(); - - for (Map.Entry entry : states.entrySet()) { - for (VirtualMachineGuru vmGuru : vmGurus) { - String name = entry.getKey(); - - VMInstanceVO vm = vmGuru.findByName(name); - - if (vm != null) { - map.put(vm.getId(), new AgentVmInfo(entry.getKey(), vmGuru, vm, entry.getValue())); - break; - } - - Long id = vmGuru.convertToId(name); - if (id != null) { - map.put(id, new AgentVmInfo(entry.getKey(), vmGuru, null,entry.getValue())); - break; - } - } - } - - return map; - } - - - - /** - * compareState does as its name suggests and compares the states between - * management server and agent. It returns whether something should be - * cleaned up - * - */ - protected Command compareState(long hostId, VMInstanceVO vm, final AgentVmInfo info, final boolean fullSync, boolean trackExternalChange) { - State agentState = info.state; - final String agentName = info.name; - final State serverState = vm.getState(); - final String serverName = vm.getInstanceName(); - - Command command = null; - s_logger.debug("VM " + serverName + ": cs state = " + serverState + " and realState = " + agentState); - if (s_logger.isDebugEnabled()) { - s_logger.debug("VM " + serverName + ": cs state = " + serverState + " and realState = " + agentState); - } - - if (agentState == State.Error) { - agentState = State.Stopped; - - short alertType = AlertManager.ALERT_TYPE_USERVM; - if (VirtualMachine.Type.DomainRouter.equals(vm.getType())) { - alertType = AlertManager.ALERT_TYPE_DOMAIN_ROUTER; - } else if (VirtualMachine.Type.ConsoleProxy.equals(vm.getType())) { - alertType = AlertManager.ALERT_TYPE_CONSOLE_PROXY; - } else if (VirtualMachine.Type.SecondaryStorageVm.equals(vm.getType())) { - alertType = AlertManager.ALERT_TYPE_SSVM; - } - - HostPodVO podVO = _podDao.findById(vm.getPodIdToDeployIn()); - DataCenterVO dcVO = _dcDao.findById(vm.getDataCenterIdToDeployIn()); - HostVO hostVO = _hostDao.findById(vm.getHostId()); - - String hostDesc = "name: " + hostVO.getName() + " (id:" + hostVO.getId() + "), availability zone: " + dcVO.getName() + ", pod: " + podVO.getName(); - _alertMgr.sendAlert(alertType, vm.getDataCenterIdToDeployIn(), vm.getPodIdToDeployIn(), "VM (name: " + vm.getInstanceName() + ", id: " + vm.getId() + ") stopped on host " + hostDesc - + " due to storage failure", "Virtual Machine " + vm.getInstanceName() + " (id: " + vm.getId() + ") running on host [" + vm.getHostId() + "] stopped due to storage failure."); - } - - if (trackExternalChange) { - if (serverState == State.Starting) { - if (vm.getHostId() != null && vm.getHostId() != hostId) { - s_logger.info("CloudStack is starting VM on host " + vm.getHostId() + ", but status report comes from a different host " + hostId + ", skip status sync for vm: " - + vm.getInstanceName()); - return null; - } - } - if (vm.getHostId() == null || hostId != vm.getHostId()) { - try { - stateTransitTo(vm, VirtualMachine.Event.AgentReportMigrated, hostId); - } catch (NoTransitionException e) { - } - } - } - - // during VM migration time, don't sync state will agent status update - if (serverState == State.Migrating) { - s_logger.debug("Skipping vm in migrating state: " + vm); - return null; - } - - if (trackExternalChange) { - if (serverState == State.Starting) { - if (vm.getHostId() != null && vm.getHostId() != hostId) { - s_logger.info("CloudStack is starting VM on host " + vm.getHostId() + ", but status report comes from a different host " + hostId + ", skip status sync for vm: " - + vm.getInstanceName()); - return null; - } - } - - if (serverState == State.Running) { - try { - // - // we had a bug that sometimes VM may be at Running State - // but host_id is null, we will cover it here. - // means that when CloudStack DB lost of host information, - // we will heal it with the info reported from host - // - if (vm.getHostId() == null || hostId != vm.getHostId()) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("detected host change when VM " + vm + " is at running state, VM could be live-migrated externally from host " + vm.getHostId() + " to host " + hostId); - } - - stateTransitTo(vm, VirtualMachine.Event.AgentReportMigrated, hostId); - } - } catch (NoTransitionException e) { - s_logger.warn(e.getMessage()); - } - } - } - - if (agentState == serverState) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Both states are " + agentState + " for " + vm); - } - assert (agentState == State.Stopped || agentState == State.Running) : "If the states we send up is changed, this must be changed."; - if (agentState == State.Running) { - try { - stateTransitTo(vm, VirtualMachine.Event.AgentReportRunning, hostId); - } catch (NoTransitionException e) { - s_logger.warn(e.getMessage()); - } - // FIXME: What if someone comes in and sets it to stopping? Then - // what? - return null; - } - - s_logger.debug("State matches but the agent said stopped so let's send a cleanup command anyways."); - return cleanup(agentName); - } - - if (agentState == State.Shutdowned) { - if (serverState == State.Running || serverState == State.Starting || serverState == State.Stopping) { - try { - advanceStop(vm, true, _accountMgr.getSystemUser(), _accountMgr.getSystemAccount()); - } catch (AgentUnavailableException e) { - assert (false) : "How do we hit this with forced on?"; - return null; - } catch (OperationTimedoutException e) { - assert (false) : "How do we hit this with forced on?"; - return null; - } catch (ConcurrentOperationException e) { - assert (false) : "How do we hit this with forced on?"; - return null; - } - } else { - s_logger.debug("Sending cleanup to a shutdowned vm: " + agentName); - command = cleanup(agentName); - } - } else if (agentState == State.Stopped) { - // This state means the VM on the agent was detected previously - // and now is gone. This is slightly different than if the VM - // was never completed but we still send down a Stop Command - // to ensure there's cleanup. - if (serverState == State.Running) { - // Our records showed that it should be running so let's restart - // it. - _haMgr.scheduleRestart(vm, false); - } else if (serverState == State.Stopping) { - _haMgr.scheduleStop(vm, hostId, WorkType.ForceStop); - s_logger.debug("Scheduling a check stop for VM in stopping mode: " + vm); - } else if (serverState == State.Starting) { - s_logger.debug("Ignoring VM in starting mode: " + vm.getInstanceName()); - _haMgr.scheduleRestart(vm, false); - } - command = cleanup(agentName); - } else if (agentState == State.Running) { - if (serverState == State.Starting) { - if (fullSync) { - try { - ensureVmRunningContext(hostId, vm, Event.AgentReportRunning); - } catch (OperationTimedoutException e) { - s_logger.error("Exception during update for running vm: " + vm, e); - return null; - } catch (ResourceUnavailableException e) { - s_logger.error("Exception during update for running vm: " + vm, e); - return null; - }catch (InsufficientAddressCapacityException e) { - s_logger.error("Exception during update for running vm: " + vm, e); - return null; - }catch (NoTransitionException e) { - s_logger.warn(e.getMessage()); - } - } - } else if (serverState == State.Stopping) { - s_logger.debug("Scheduling a stop command for " + vm); - _haMgr.scheduleStop(vm, hostId, WorkType.Stop); - } else { - s_logger.debug("server VM state " + serverState + " does not meet expectation of a running VM report from agent"); - - // just be careful not to stop VM for things we don't handle - // command = cleanup(agentName); - } - } - return command; - } - - private void ensureVmRunningContext(long hostId, VMInstanceVO vm, Event cause) throws OperationTimedoutException, ResourceUnavailableException, NoTransitionException, InsufficientAddressCapacityException { - VirtualMachineGuru vmGuru = getVmGuru(vm); - - s_logger.debug("VM state is starting on full sync so updating it to running"); - vm = findByIdAndType(vm.getType(), vm.getId()); - - // grab outstanding work item if any - ItWorkVO work = _workDao.findByOutstandingWork(vm.getId(), vm.getState()); - if (work != null) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Found an outstanding work item for this vm " + vm + " in state:" + vm.getState() + ", work id:" + work.getId()); - } - } - - try { - stateTransitTo(vm, cause, hostId); - } catch (NoTransitionException e1) { - s_logger.warn(e1.getMessage()); - } - - s_logger.debug("VM's " + vm + " state is starting on full sync so updating it to Running"); - vm = vmGuru.findById(vm.getId()); // this should ensure vm has the most - // up to date info - - VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm); - List nics = _nicsDao.listByVmId(profile.getId()); - for (NicVO nic : nics) { - Network network = _networkMgr.getNetwork(nic.getNetworkId()); - NicProfile nicProfile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), null, - _networkMgr.isSecurityGroupSupportedInNetwork(network), _networkMgr.getNetworkTag(profile.getHypervisorType(), network)); - profile.addNic(nicProfile); - } - - Commands cmds = new Commands(OnError.Stop); - s_logger.debug("Finalizing commands that need to be send to complete Start process for the vm " + vm); - - if (vmGuru.finalizeCommandsOnStart(cmds, profile)) { - if (cmds.size() != 0) { - _agentMgr.send(vm.getHostId(), cmds); - } - - if (vmGuru.finalizeStart(profile, vm.getHostId(), cmds, null)) { - stateTransitTo(vm, cause, vm.getHostId()); - } else { - s_logger.error("Unable to finish finialization for running vm: " + vm); - } - } else { - s_logger.error("Unable to finalize commands on start for vm: " + vm); - } - - if (work != null) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Updating outstanding work item to Done, id:" + work.getId()); - } - work.setStep(Step.Done); - _workDao.update(work.getId(), work); - } - } - - @Override - public boolean isRecurring() { - return true; - } - - @Override - public boolean processAnswers(long agentId, long seq, Answer[] answers) { - for (final Answer answer : answers) { - if (answer instanceof ClusterSyncAnswer) { - ClusterSyncAnswer hs = (ClusterSyncAnswer) answer; - if (!hs.isExceuted()){ - deltaSync(hs.getNewStates()); - hs.setExecuted(); - } - } - } - return true; - } - - @Override - public boolean processTimeout(long agentId, long seq) { - return true; - } - - @Override - public int getTimeout() { - return -1; - } - - @Override - public boolean processCommands(long agentId, long seq, Command[] cmds) { - boolean processed = false; - for (Command cmd : cmds) { - if (cmd instanceof PingRoutingCommand) { - PingRoutingCommand ping = (PingRoutingCommand) cmd; - if (ping.getNewStates() != null && ping.getNewStates().size() > 0) { - Commands commands = deltaHostSync(agentId, ping.getNewStates()); - if (commands.size() > 0) { - try { - _agentMgr.send(agentId, commands, this); - } catch (final AgentUnavailableException e) { - s_logger.warn("Agent is now unavailable", e); - } - } - } - processed = true; - } - } - return processed; - } - - @Override - public AgentControlAnswer processControlCommand(long agentId, AgentControlCommand cmd) { - return null; - } - - @Override - public boolean processDisconnect(long agentId, Status state) { - return true; - } - - @Override - public void processConnect(HostVO agent, StartupCommand cmd, boolean forRebalance) throws ConnectionException { - if (!(cmd instanceof StartupRoutingCommand)) { - return; - } - - if (forRebalance) { - s_logger.debug("Not processing listener " + this + " as connect happens on rebalance process"); - return; - } - - if (forRebalance) { - s_logger.debug("Not processing listener " + this + " as connect happens on rebalance process"); - return; - } - - 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 - ClusterSyncCommand syncCmd = new ClusterSyncCommand(Integer.parseInt(Config.ClusterDeltaSyncInterval.getDefaultValue()), clusterId); - try { - long seq_no = _agentMgr.send(agentId, new Commands(syncCmd), this); - s_logger.debug("Cluster VM sync started with jobid " + seq_no); - } catch (AgentUnavailableException e) { - s_logger.fatal("The Cluster VM sync process failed for cluster id " + clusterId + " with ", e); - } - } - else { // for others KVM and VMWare - StartupRoutingCommand startup = (StartupRoutingCommand) cmd; - Commands commands = fullHostSync(agentId, startup); - - if (commands.size() > 0) { - s_logger.debug("Sending clean commands to the agent"); - - try { - boolean error = false; - Answer[] answers = _agentMgr.send(agentId, commands); - for (Answer answer : answers) { - if (!answer.getResult()) { - s_logger.warn("Unable to stop a VM due to " + answer.getDetails()); - error = true; - } - } - if (error) { - throw new ConnectionException(true, "Unable to stop VMs"); - } - } catch (final AgentUnavailableException e) { - s_logger.warn("Agent is unavailable now", e); - throw new ConnectionException(true, "Unable to sync", e); - } catch (final OperationTimedoutException e) { - s_logger.warn("Agent is unavailable now", e); - throw new ConnectionException(true, "Unable to sync", e); - } - } - - } - } - - protected class TransitionTask implements Runnable { - @Override - public void run() { - GlobalLock lock = GlobalLock.getInternLock("TransitionChecking"); - if (lock == null) { - s_logger.debug("Couldn't get the global lock"); - return; - } - - if (!lock.lock(30)) { - s_logger.debug("Couldn't lock the db"); - return; - } - try { - lock.addRef(); - List instances = _vmDao.findVMInTransition(new Date(new Date().getTime() - (_operationTimeout * 1000)), State.Starting, State.Stopping); - for (VMInstanceVO instance : instances) { - State state = instance.getState(); - if (state == State.Stopping) { - _haMgr.scheduleStop(instance, instance.getHostId(), WorkType.CheckStop); - } else if (state == State.Starting) { - _haMgr.scheduleRestart(instance, true); - } - } - } catch (Exception e) { - s_logger.warn("Caught the following exception on transition checking", e); - } finally { - StackMaid.current().exitCleanup(); - lock.unlock(); - } - } - } - - protected class AgentVmInfo { - public String name; - public State state; - public String hostUuid; - public VMInstanceVO vm; - public VirtualMachineGuru guru; - - @SuppressWarnings("unchecked") - public AgentVmInfo(String name, VirtualMachineGuru guru, VMInstanceVO vm, State state, String host) { - this.name = name; - this.state = state; - this.vm = vm; - this.guru = (VirtualMachineGuru) guru; - this.hostUuid = host; - } - - public AgentVmInfo(String name, VirtualMachineGuru guru, VMInstanceVO vm, State state) { - this(name, guru, vm, state, null); - } - - public String getHostUuid() { - return hostUuid; - } - } - - @Override - 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 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.", 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() + ")"); - } - - throw new InvalidParameterValueException("Not upgrading vm " + vmInstance.toString() + " since it already " + - "has the requested service offering (" + newServiceOffering.getName() + ")", null); - } - - ServiceOfferingVO currentServiceOffering = _offeringDao.findByIdIncludingRemoved(vmInstance.getServiceOfferingId()); - - // Check that the service offering being upgraded to has the same Guest IP type as the VM's current service offering - // NOTE: With the new network refactoring in 2.2, we shouldn't need the check for same guest IP type anymore. - /* - * if (!currentServiceOffering.getGuestIpType().equals(newServiceOffering.getGuestIpType())) { String errorMsg = - * "The service offering being upgraded to has a guest IP type: " + newServiceOffering.getGuestIpType(); errorMsg += - * ". Please select a service offering with the same guest IP type as the VM's current service offering (" + - * currentServiceOffering.getGuestIpType() + ")."; throw new InvalidParameterValueException(errorMsg); } - */ - - // Check that the service offering being upgraded to has the same storage pool preference as the VM's current service - // offering - if (currentServiceOffering.getUseLocalStorage() != newServiceOffering.getUseLocalStorage()) { - 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(), 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", 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", null); - } - - // Check that the service offering being upgraded to has all the tags of the current service offering - List currentTags = _configMgr.csvTagsToList(currentServiceOffering.getTags()); - 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 " - + "current service offering. Current service offering tags: " + currentTags + "; " + "new service " + - "offering tags: " + newTags, null); - } - } - - @Override - public boolean upgradeVmDb(long vmId, long serviceOfferingId) { - VMInstanceVO vmForUpdate = _vmDao.createForUpdate(); - vmForUpdate.setServiceOfferingId(serviceOfferingId); - ServiceOffering newSvcOff = _configMgr.getServiceOffering(serviceOfferingId); - vmForUpdate.setHaEnabled(newSvcOff.getOfferHA()); - vmForUpdate.setLimitCpuUse(newSvcOff.getLimitCpuUse()); - vmForUpdate.setServiceOfferingId(newSvcOff.getId()); - return _vmDao.update(vmId, vmForUpdate); - } - - @Override - public NicProfile addVmToNetwork(VirtualMachine vm, Network network, NicProfile requested) throws ConcurrentOperationException, - 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"); - return nic; - } else { - s_logger.warn("Failed to plug nic to the vm " + vm + " in network " + network); - return null; - } - } else if (vm.getState() == State.Stopped) { - //1) allocate nic - return _networkMgr.createNicForVm(network, requested, context, vmProfile, false); - } else { - s_logger.warn("Unable to add vm " + vm + " to network " + network); - throw new ResourceUnavailableException("Unable to add vm " + vm + " to network, is not in the right state", - DataCenter.class, vm.getDataCenterIdToDeployIn()); - } - } - - - @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); - boolean result = vmGuru.unplugNic(network, nicTO, vmTO, context, dest); - if (result) { - s_logger.debug("Nic is unplugged successfully for vm " + vm + " in network " + network ); - } else { - 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; - } - -} +// 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.vm; + +import java.net.URI; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Date; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.UUID; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +import javax.ejb.Local; +import javax.naming.ConfigurationException; + +import org.apache.log4j.Logger; + +import com.cloud.agent.AgentManager; +import com.cloud.agent.AgentManager.OnError; +import com.cloud.agent.Listener; +import com.cloud.agent.api.AgentControlAnswer; +import com.cloud.agent.api.AgentControlCommand; +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.CheckVirtualMachineAnswer; +import com.cloud.agent.api.CheckVirtualMachineCommand; +import com.cloud.agent.api.ClusterSyncAnswer; +import com.cloud.agent.api.ClusterSyncCommand; +import com.cloud.agent.api.Command; +import com.cloud.agent.api.MigrateAnswer; +import com.cloud.agent.api.MigrateCommand; +import com.cloud.agent.api.PingRoutingCommand; +import com.cloud.agent.api.PrepareForMigrationAnswer; +import com.cloud.agent.api.PrepareForMigrationCommand; +import com.cloud.agent.api.RebootAnswer; +import com.cloud.agent.api.RebootCommand; +import com.cloud.agent.api.StartAnswer; +import com.cloud.agent.api.StartCommand; +import com.cloud.agent.api.StartupCommand; +import com.cloud.agent.api.StartupRoutingCommand; +import com.cloud.agent.api.StartupRoutingCommand.VmState; +import com.cloud.agent.api.StopAnswer; +import com.cloud.agent.api.StopCommand; +import com.cloud.agent.api.to.NicTO; +import com.cloud.agent.api.to.VirtualMachineTO; +import com.cloud.agent.manager.Commands; +import com.cloud.agent.manager.allocator.HostAllocator; +import com.cloud.alert.AlertManager; +import com.cloud.capacity.CapacityManager; +import com.cloud.cluster.ClusterManager; +import com.cloud.cluster.StackMaid; +import com.cloud.configuration.Config; +import com.cloud.configuration.ConfigurationManager; +import com.cloud.configuration.dao.ConfigurationDao; +import com.cloud.consoleproxy.ConsoleProxyManager; +import com.cloud.dc.DataCenter; +import com.cloud.dc.DataCenterVO; +import com.cloud.dc.HostPodVO; +import com.cloud.dc.dao.DataCenterDao; +import com.cloud.dc.dao.HostPodDao; +import com.cloud.deploy.DataCenterDeployment; +import com.cloud.deploy.DeployDestination; +import com.cloud.deploy.DeploymentPlan; +import com.cloud.deploy.DeploymentPlanner; +import com.cloud.deploy.DeploymentPlanner.ExcludeList; +import com.cloud.domain.dao.DomainDao; +import com.cloud.exception.AgentUnavailableException; +import com.cloud.exception.ConcurrentOperationException; +import com.cloud.exception.ConnectionException; +import com.cloud.exception.InsufficientAddressCapacityException; +import com.cloud.exception.InsufficientCapacityException; +import com.cloud.exception.InsufficientServerCapacityException; +import com.cloud.exception.InsufficientVirtualNetworkCapcityException; +import com.cloud.exception.InvalidParameterValueException; +import com.cloud.exception.ManagementServerException; +import com.cloud.exception.OperationTimedoutException; +import com.cloud.exception.ResourceUnavailableException; +import com.cloud.exception.VirtualMachineMigrationException; +import com.cloud.ha.HighAvailabilityManager; +import com.cloud.ha.HighAvailabilityManager.WorkType; +import com.cloud.host.Host; +import com.cloud.host.HostVO; +import com.cloud.host.Status; +import com.cloud.host.dao.HostDao; +import com.cloud.hypervisor.Hypervisor.HypervisorType; +import com.cloud.hypervisor.HypervisorGuru; +import com.cloud.hypervisor.HypervisorGuruManager; +import com.cloud.network.Network; +import com.cloud.network.NetworkManager; +import com.cloud.network.NetworkVO; +import com.cloud.network.dao.NetworkDao; +import com.cloud.offering.ServiceOffering; +import com.cloud.org.Cluster; +import com.cloud.resource.ResourceManager; +import com.cloud.service.ServiceOfferingVO; +import com.cloud.service.dao.ServiceOfferingDao; +import com.cloud.storage.DiskOfferingVO; +import com.cloud.storage.Storage.ImageFormat; +import com.cloud.storage.StorageManager; +import com.cloud.storage.StoragePool; +import com.cloud.storage.StoragePoolVO; +import com.cloud.storage.VMTemplateVO; +import com.cloud.storage.Volume; +import com.cloud.storage.Volume.Type; +import com.cloud.storage.VolumeVO; +import com.cloud.storage.dao.GuestOSCategoryDao; +import com.cloud.storage.dao.GuestOSDao; +import com.cloud.storage.dao.StoragePoolDao; +import com.cloud.storage.dao.VMTemplateDao; +import com.cloud.storage.dao.VolumeDao; +import com.cloud.user.Account; +import com.cloud.user.AccountManager; +import com.cloud.user.User; +import com.cloud.user.dao.AccountDao; +import com.cloud.user.dao.UserDao; +import com.cloud.utils.Journal; +import com.cloud.utils.NumbersUtil; +import com.cloud.utils.Pair; +import com.cloud.utils.Ternary; +import com.cloud.utils.component.Adapters; +import com.cloud.utils.component.ComponentLocator; +import com.cloud.utils.component.Inject; +import com.cloud.utils.concurrency.NamedThreadFactory; +import com.cloud.utils.db.DB; +import com.cloud.utils.db.GlobalLock; +import com.cloud.utils.db.Transaction; +import com.cloud.utils.exception.CloudRuntimeException; +import com.cloud.utils.exception.ExecutionException; +import com.cloud.utils.fsm.NoTransitionException; +import com.cloud.utils.fsm.StateMachine2; +import com.cloud.vm.ItWorkVO.Step; +import com.cloud.vm.VirtualMachine.Event; +import com.cloud.vm.VirtualMachine.State; +import com.cloud.vm.dao.ConsoleProxyDao; +import com.cloud.vm.dao.DomainRouterDao; +import com.cloud.vm.dao.NicDao; +import com.cloud.vm.dao.SecondaryStorageVmDao; +import com.cloud.vm.dao.UserVmDao; +import com.cloud.vm.dao.VMInstanceDao; + +@Local(value = VirtualMachineManager.class) +public class VirtualMachineManagerImpl implements VirtualMachineManager, Listener { + private static final Logger s_logger = Logger.getLogger(VirtualMachineManagerImpl.class); + + String _name; + @Inject + protected StorageManager _storageMgr; + @Inject + protected NetworkManager _networkMgr; + @Inject + protected AgentManager _agentMgr; + @Inject + protected VMInstanceDao _vmDao; + @Inject + protected ServiceOfferingDao _offeringDao; + @Inject + protected VMTemplateDao _templateDao; + @Inject + protected UserDao _userDao; + @Inject + protected AccountDao _accountDao; + @Inject + protected DomainDao _domainDao; + @Inject + protected ClusterManager _clusterMgr; + @Inject + protected ItWorkDao _workDao; + @Inject + protected UserVmDao _userVmDao; + @Inject + protected DomainRouterDao _routerDao; + @Inject + protected ConsoleProxyDao _consoleDao; + @Inject + protected SecondaryStorageVmDao _secondaryDao; + @Inject + protected NicDao _nicsDao; + @Inject + protected AccountManager _accountMgr; + @Inject + protected HostDao _hostDao; + @Inject + protected AlertManager _alertMgr; + @Inject + protected GuestOSCategoryDao _guestOsCategoryDao; + @Inject + protected GuestOSDao _guestOsDao; + @Inject + protected VolumeDao _volsDao; + @Inject + protected ConsoleProxyManager _consoleProxyMgr; + @Inject + protected ConfigurationManager _configMgr; + @Inject + protected CapacityManager _capacityMgr; + @Inject + protected HighAvailabilityManager _haMgr; + @Inject + protected HostPodDao _podDao; + @Inject + protected DataCenterDao _dcDao; + @Inject + protected StoragePoolDao _storagePoolDao; + @Inject + protected HypervisorGuruManager _hvGuruMgr; + @Inject + protected NetworkDao _networkDao; + + @Inject(adapter = DeploymentPlanner.class) + protected Adapters _planners; + + @Inject(adapter = HostAllocator.class) + protected Adapters _hostAllocators; + + @Inject + protected ResourceManager _resourceMgr; + + Map> _vmGurus = new HashMap>(); + protected StateMachine2 _stateMachine; + + ScheduledExecutorService _executor = null; + protected int _operationTimeout; + + protected int _retry; + protected long _nodeId; + protected long _cleanupWait; + protected long _cleanupInterval; + protected long _cancelWait; + protected long _opWaitInterval; + protected int _lockStateRetry; + protected boolean _forceStop; + + @Override + public void registerGuru(VirtualMachine.Type type, VirtualMachineGuru guru) { + synchronized (_vmGurus) { + _vmGurus.put(type, guru); + } + } + + @Override + @DB + public T allocate(T vm, VMTemplateVO template, ServiceOfferingVO serviceOffering, Pair rootDiskOffering, + List> dataDiskOfferings, List> networks, Map params, DeploymentPlan plan, + HypervisorType hyperType, Account owner) throws InsufficientCapacityException { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Allocating entries for VM: " + vm); + } + + VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vm, template, serviceOffering, owner, params); + + vm.setDataCenterId(plan.getDataCenterId()); + if (plan.getPodId() != null) { + vm.setPodId(plan.getPodId()); + } + assert (plan.getClusterId() == null && plan.getPoolId() == null) : "We currently don't support cluster and pool preset yet"; + + @SuppressWarnings("unchecked") + VirtualMachineGuru guru = (VirtualMachineGuru) _vmGurus.get(vm.getType()); + + Transaction txn = Transaction.currentTxn(); + txn.start(); + vm = guru.persist(vm); + + if (s_logger.isDebugEnabled()) { + s_logger.debug("Allocating nics for " + vm); + } + + try { + _networkMgr.allocate(vmProfile, networks); + } catch (ConcurrentOperationException e) { + throw new CloudRuntimeException("Concurrent operation while trying to allocate resources for the VM", e); + } + + if (dataDiskOfferings == null) { + dataDiskOfferings = new ArrayList>(0); + } + + if (s_logger.isDebugEnabled()) { + s_logger.debug("Allocaing disks for " + vm); + } + + if (template.getFormat() == ImageFormat.ISO) { + _storageMgr.allocateRawVolume(Type.ROOT, "ROOT-" + vm.getId(), rootDiskOffering.first(), rootDiskOffering.second(), vm, owner); + } else if (template.getFormat() == ImageFormat.BAREMETAL) { + // Do nothing + } else { + _storageMgr.allocateTemplatedVolume(Type.ROOT, "ROOT-" + vm.getId(), rootDiskOffering.first(), template, vm, owner); + } + + for (Pair offering : dataDiskOfferings) { + _storageMgr.allocateRawVolume(Type.DATADISK, "DATA-" + vm.getId(), offering.first(), offering.second(), vm, owner); + } + + txn.commit(); + if (s_logger.isDebugEnabled()) { + s_logger.debug("Allocation completed for VM: " + vm); + } + + return vm; + } + + @Override + public T allocate(T vm, VMTemplateVO template, ServiceOfferingVO serviceOffering, Long rootSize, Pair dataDiskOffering, + List> networks, DeploymentPlan plan, HypervisorType hyperType, Account owner) throws InsufficientCapacityException { + List> diskOfferings = new ArrayList>(1); + if (dataDiskOffering != null) { + diskOfferings.add(dataDiskOffering); + } + return allocate(vm, template, serviceOffering, new Pair(serviceOffering, rootSize), diskOfferings, networks, null, plan, hyperType, owner); + } + + @Override + public T allocate(T vm, VMTemplateVO template, ServiceOfferingVO serviceOffering, List> networks, DeploymentPlan plan, + HypervisorType hyperType, Account owner) throws InsufficientCapacityException { + return allocate(vm, template, serviceOffering, new Pair(serviceOffering, null), null, networks, null, plan, hyperType, owner); + } + + @SuppressWarnings("unchecked") + private VirtualMachineGuru getVmGuru(T vm) { + return (VirtualMachineGuru) _vmGurus.get(vm.getType()); + } + + @SuppressWarnings("unchecked") + private VirtualMachineGuru getBareMetalVmGuru(T vm) { + return (VirtualMachineGuru) _vmGurus.get(VirtualMachine.Type.UserBareMetal); + } + + @Override + public boolean expunge(T vm, User caller, Account account) throws ResourceUnavailableException { + try { + if (advanceExpunge(vm, caller, account)) { + // Mark vms as removed + remove(vm, caller, account); + return true; + } else { + s_logger.info("Did not expunge " + vm); + return false; + } + } catch (OperationTimedoutException e) { + throw new CloudRuntimeException("Operation timed out", e); + } catch (ConcurrentOperationException e) { + throw new CloudRuntimeException("Concurrent operation ", e); + } + } + + @Override + public boolean advanceExpunge(T vm, User caller, Account account) throws ResourceUnavailableException, OperationTimedoutException, ConcurrentOperationException { + if (vm == null || vm.getRemoved() != null) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Unable to find vm or vm is destroyed: " + vm); + } + return true; + } + + if (!this.advanceStop(vm, false, caller, account)) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Unable to stop the VM so we can't expunge it."); + } + } + + try { + if (!stateTransitTo(vm, VirtualMachine.Event.ExpungeOperation, vm.getHostId())) { + s_logger.debug("Unable to destroy the vm because it is not in the correct state: " + vm); + return false; + } + } catch (NoTransitionException e) { + s_logger.debug("Unable to destroy the vm because it is not in the correct state: " + vm); + return false; + } + + if (s_logger.isDebugEnabled()) { + s_logger.debug("Destroying vm " + vm); + } + + VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm); + s_logger.debug("Cleaning up NICS"); + _networkMgr.cleanupNics(profile); + // Clean up volumes based on the vm's instance id + _storageMgr.cleanupVolumes(vm.getId()); + + VirtualMachineGuru guru = getVmGuru(vm); + guru.finalizeExpunge(vm); + + if (s_logger.isDebugEnabled()) { + s_logger.debug("Expunged " + vm); + } + + return true; + } + + @Override + public boolean start() { + _executor.scheduleAtFixedRate(new CleanupTask(), _cleanupInterval, _cleanupInterval, TimeUnit.SECONDS); + cancelWorkItems(_nodeId); + return true; + } + + @Override + public boolean stop() { + return true; + } + + @Override + public boolean configure(String name, Map xmlParams) throws ConfigurationException { + _name = name; + + ComponentLocator locator = ComponentLocator.getCurrentLocator(); + ConfigurationDao configDao = locator.getDao(ConfigurationDao.class); + Map params = configDao.getConfiguration(xmlParams); + + _retry = NumbersUtil.parseInt(params.get(Config.StartRetry.key()), 10); + + ReservationContextImpl.setComponents(_userDao, _domainDao, _accountDao); + VirtualMachineProfileImpl.setComponents(_offeringDao, _templateDao, _accountDao); + + _cancelWait = NumbersUtil.parseLong(params.get(Config.VmOpCancelInterval.key()), 3600); + _cleanupWait = NumbersUtil.parseLong(params.get(Config.VmOpCleanupWait.key()), 3600); + _cleanupInterval = NumbersUtil.parseLong(params.get(Config.VmOpCleanupInterval.key()), 86400) * 1000; + _opWaitInterval = NumbersUtil.parseLong(params.get(Config.VmOpWaitInterval.key()), 120) * 1000; + _lockStateRetry = NumbersUtil.parseInt(params.get(Config.VmOpLockStateRetry.key()), 5); + _operationTimeout = NumbersUtil.parseInt(params.get(Config.Wait.key()), 1800) * 2; + _forceStop = Boolean.parseBoolean(params.get(Config.VmDestroyForcestop.key())); + + _executor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("Vm-Operations-Cleanup")); + _nodeId = _clusterMgr.getManagementNodeId(); + + _agentMgr.registerForHostEvents(this, true, true, true); + + return true; + } + + @Override + public String getName() { + return _name; + } + + protected VirtualMachineManagerImpl() { + setStateMachine(); + } + + @Override + public T start(T vm, Map params, User caller, Account account) throws InsufficientCapacityException, ResourceUnavailableException { + return start(vm, params, caller, account, null); + } + + @Override + public T start(T vm, Map params, User caller, Account account, DeploymentPlan planToDeploy) throws InsufficientCapacityException, + ResourceUnavailableException { + try { + return advanceStart(vm, params, caller, account, planToDeploy); + } catch (ConcurrentOperationException e) { + throw new CloudRuntimeException("Unable to start a VM due to concurrent operation", e); + } + } + + protected boolean checkWorkItems(VMInstanceVO vm, State state) throws ConcurrentOperationException { + while (true) { + ItWorkVO vo = _workDao.findByOutstandingWork(vm.getId(), state); + if (vo == null) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Unable to find work for VM: " + vm + " and state: " + state); + } + return true; + } + + if (vo.getStep() == Step.Done) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Work for " + vm + " is " + vo.getStep()); + } + return true; + } + + if (vo.getSecondsTaskIsInactive() > _cancelWait) { + s_logger.warn("The task item for vm " + vm + " has been inactive for " + vo.getSecondsTaskIsInactive()); + return false; + } + + try { + Thread.sleep(_opWaitInterval); + } catch (InterruptedException e) { + s_logger.info("Waiting for " + vm + " but is interrupted"); + throw new ConcurrentOperationException("Waiting for " + vm + " but is interrupted"); + } + s_logger.debug("Waiting some more to make sure there's no activity on " + vm); + } + + } + + @DB + protected Ternary changeToStartState(VirtualMachineGuru vmGuru, T vm, User caller, Account account) + throws ConcurrentOperationException { + long vmId = vm.getId(); + + ItWorkVO work = new ItWorkVO(UUID.randomUUID().toString(), _nodeId, State.Starting, vm.getType(), vm.getId()); + int retry = _lockStateRetry; + while (retry-- != 0) { + Transaction txn = Transaction.currentTxn(); + Ternary result = null; + txn.start(); + try { + Journal journal = new Journal.LogJournal("Creating " + vm, s_logger); + work = _workDao.persist(work); + ReservationContextImpl context = new ReservationContextImpl(work.getId(), journal, caller, account); + + if (stateTransitTo(vm, Event.StartRequested, null, work.getId())) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Successfully transitioned to start state for " + vm + " reservation id = " + work.getId()); + } + result = new Ternary(vmGuru.findById(vmId), context, work); + txn.commit(); + return result; + } + } catch (NoTransitionException e) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Unable to transition into Starting state due to " + e.getMessage()); + } + } finally { + if (result == null) { + txn.rollback(); + } + } + + VMInstanceVO instance = _vmDao.findById(vmId); + if (instance == null) { + throw new ConcurrentOperationException("Unable to acquire lock on " + vm); + } + + if (s_logger.isDebugEnabled()) { + s_logger.debug("Determining why we're unable to update the state to Starting for " + instance + ". Retry=" + retry); + } + + State state = instance.getState(); + if (state == State.Running) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("VM is already started: " + vm); + } + return null; + } + + if (state.isTransitional()) { + if (!checkWorkItems(vm, state)) { + throw new ConcurrentOperationException("There are concurrent operations on " + vm); + } else { + continue; + } + } + + if (state != State.Stopped) { + s_logger.debug("VM " + vm + " is not in a state to be started: " + state); + return null; + } + } + + throw new ConcurrentOperationException("Unable to change the state of " + vm); + } + + protected boolean changeState(T vm, Event event, Long hostId, ItWorkVO work, Step step) throws NoTransitionException { + // FIXME: We should do this better. + Step previousStep = work.getStep(); + _workDao.updateStep(work, step); + boolean result = false; + try { + result = stateTransitTo(vm, event, hostId); + return result; + } finally { + if (!result) { + _workDao.updateStep(work, previousStep); + } + } + } + + @Override + public T advanceStart(T vm, Map params, User caller, Account account) throws InsufficientCapacityException, + ConcurrentOperationException, ResourceUnavailableException { + return advanceStart(vm, params, caller, account, null); + } + + @Override + public T advanceStart(T vm, Map params, User caller, Account account, DeploymentPlan planToDeploy) + throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException { + long vmId = vm.getId(); + VirtualMachineGuru vmGuru; + if (vm.getHypervisorType() == HypervisorType.BareMetal) { + vmGuru = getBareMetalVmGuru(vm); + } else { + vmGuru = getVmGuru(vm); + } + + vm = vmGuru.findById(vm.getId()); + Ternary start = changeToStartState(vmGuru, vm, caller, account); + if (start == null) { + return vmGuru.findById(vmId); + } + + vm = start.first(); + ReservationContext ctx = start.second(); + ItWorkVO work = start.third(); + + T startedVm = null; + ServiceOfferingVO offering = _offeringDao.findById(vm.getServiceOfferingId()); + VMTemplateVO template = _templateDao.findById(vm.getTemplateId()); + + if (s_logger.isDebugEnabled()) { + s_logger.debug("Trying to deploy VM, vm has dcId: " + vm.getDataCenterIdToDeployIn() + " and podId: " + vm.getPodIdToDeployIn()); + } + DataCenterDeployment plan = new DataCenterDeployment(vm.getDataCenterIdToDeployIn(), vm.getPodIdToDeployIn(), null, null, null, null, ctx); + if(planToDeploy != null && planToDeploy.getDataCenterId() != 0){ + if (s_logger.isDebugEnabled()) { + s_logger.debug("advanceStart: DeploymentPlan is provided, using dcId:" + planToDeploy.getDataCenterId() + ", podId: " + planToDeploy.getPodId() + ", clusterId: " + + planToDeploy.getClusterId() + ", hostId: " + planToDeploy.getHostId() + ", poolId: " + planToDeploy.getPoolId()); + } + plan = new DataCenterDeployment(planToDeploy.getDataCenterId(), planToDeploy.getPodId(), planToDeploy.getClusterId(), planToDeploy.getHostId(), planToDeploy.getPoolId(), planToDeploy.getPhysicalNetworkId(), ctx); + } + + HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vm.getHypervisorType()); + + boolean canRetry = true; + try { + Journal journal = start.second().getJournal(); + + ExcludeList avoids = null; + if (planToDeploy != null) { + avoids = planToDeploy.getAvoids(); + } + if (avoids == null) { + avoids = new ExcludeList(); + } + if (s_logger.isDebugEnabled()) { + s_logger.debug("Deploy avoids pods: " + avoids.getPodsToAvoid() + ", clusters: " + avoids.getClustersToAvoid() + ", hosts: " + avoids.getHostsToAvoid()); + } + + + boolean planChangedByVolume = false; + boolean reuseVolume = true; + DataCenterDeployment originalPlan = plan; + + int retry = _retry; + boolean recreate = false; + while (retry-- != 0) { // It's != so that it can match -1. + + if(reuseVolume){ + // edit plan if this vm's ROOT volume is in READY state already + List vols = _volsDao.findReadyRootVolumesByInstance(vm.getId()); + for (VolumeVO vol : vols) { + // make sure if the templateId is unchanged. If it is changed, + // let planner + // reassign pool for the volume even if it ready. + Long volTemplateId = vol.getTemplateId(); + if (volTemplateId != null && volTemplateId.longValue() != template.getId()) { + if (s_logger.isDebugEnabled()) { + s_logger.debug(vol + " of " + vm + " is READY, but template ids don't match, let the planner reassign a new pool"); + } + continue; + } + + StoragePoolVO pool = _storagePoolDao.findById(vol.getPoolId()); + if (!pool.isInMaintenance()) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Root volume is ready, need to place VM in volume's cluster"); + } + long rootVolDcId = pool.getDataCenterId(); + Long rootVolPodId = pool.getPodId(); + Long rootVolClusterId = pool.getClusterId(); + if (planToDeploy != null && planToDeploy.getDataCenterId() != 0) { + Long clusterIdSpecified = planToDeploy.getClusterId(); + if (clusterIdSpecified != null && rootVolClusterId != null) { + if (rootVolClusterId.longValue() != clusterIdSpecified.longValue()) { + // cannot satisfy the plan passed in to the + // planner + if (s_logger.isDebugEnabled()) { + s_logger.debug("Cannot satisfy the deployment plan passed in since the ready Root volume is in different cluster. volume's cluster: " + rootVolClusterId + + ", cluster specified: " + clusterIdSpecified); + } + throw new ResourceUnavailableException("Root volume is ready in different cluster, Deployment plan provided cannot be satisfied, unable to create a deployment for " + + vm, Cluster.class, clusterIdSpecified); + } + } + plan = new DataCenterDeployment(planToDeploy.getDataCenterId(), planToDeploy.getPodId(), planToDeploy.getClusterId(), planToDeploy.getHostId(), vol.getPoolId(), null, ctx); + }else{ + plan = new DataCenterDeployment(rootVolDcId, rootVolPodId, rootVolClusterId, null, vol.getPoolId(), null, ctx); + if (s_logger.isDebugEnabled()) { + s_logger.debug(vol + " is READY, changing deployment plan to use this pool's dcId: " + rootVolDcId + " , podId: " + rootVolPodId + " , and clusterId: " + rootVolClusterId); + } + planChangedByVolume = true; + } + } + } + } + + VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vm, template, offering, account, params); + DeployDestination dest = null; + for (DeploymentPlanner planner : _planners) { + if (planner.canHandle(vmProfile, plan, avoids)) { + dest = planner.plan(vmProfile, plan, avoids); + } else { + continue; + } + if (dest != null) { + avoids.addHost(dest.getHost().getId()); + journal.record("Deployment found ", vmProfile, dest); + break; + } + } + + if (dest == null) { + if (planChangedByVolume) { + plan = originalPlan; + planChangedByVolume = false; + //do not enter volume reuse for next retry, since we want to look for resorces outside the volume's cluster + reuseVolume = false; + continue; + } + throw new InsufficientServerCapacityException("Unable to create a deployment for " + vmProfile, DataCenter.class, plan.getDataCenterId()); + } + + long destHostId = dest.getHost().getId(); + vm.setPodId(dest.getPod().getId()); + + try { + if (!changeState(vm, Event.OperationRetry, destHostId, work, Step.Prepare)) { + throw new ConcurrentOperationException("Unable to update the state of the Virtual Machine"); + } + } catch (NoTransitionException e1) { + throw new ConcurrentOperationException(e1.getMessage()); + } + + try { + if (s_logger.isDebugEnabled()) { + s_logger.debug("VM is being created in podId: " + vm.getPodIdToDeployIn()); + } + _networkMgr.prepare(vmProfile, dest, ctx); + if (vm.getHypervisorType() != HypervisorType.BareMetal) { + _storageMgr.prepare(vmProfile, dest, recreate); + recreate = false; + } + //since StorageMgr succeeded in volume creation, reuse Volume for further tries until current cluster has capacity + if(!reuseVolume){ + reuseVolume = true; + } + + Commands cmds = null; + vmGuru.finalizeVirtualMachineProfile(vmProfile, dest, ctx); + + VirtualMachineTO vmTO = hvGuru.implement(vmProfile); + + cmds = new Commands(OnError.Stop); + cmds.addCommand(new StartCommand(vmTO)); + + vmGuru.finalizeDeployment(cmds, vmProfile, dest, ctx); + + + work = _workDao.findById(work.getId()); + if (work == null || work.getStep() != Step.Prepare) { + throw new ConcurrentOperationException("Work steps have been changed: " + work); + } + _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(); + if( host_guid != null ) { + HostVO finalHost = _resourceMgr.findHostByGuid(host_guid); + if (finalHost == null ) { + throw new CloudRuntimeException("Host Guid " + host_guid + " doesn't exist in DB, something wrong here"); + } + destHostId = finalHost.getId(); + } + if (vmGuru.finalizeStart(vmProfile, destHostId, cmds, ctx)) { + if (!changeState(vm, Event.OperationSucceeded, destHostId, work, Step.Done)) { + throw new ConcurrentOperationException("Unable to transition to a new state."); + } + startedVm = vm; + if (s_logger.isDebugEnabled()) { + s_logger.debug("Start completed for VM " + vm); + } + return startedVm; + } else { + 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()) { + s_logger.warn("Unable to stop " + vm + " due to " + (answer != null ? answer.getDetails() : "no answers")); + _haMgr.scheduleStop(vm, destHostId, WorkType.ForceStop); + 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; + } else { + 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()) { + _haMgr.scheduleStop(vm, destHostId, WorkType.CheckStop); + } + canRetry = false; + throw new AgentUnavailableException("Unable to start " + vm.getHostName(), destHostId, e); + } catch (ResourceUnavailableException e) { + s_logger.info("Unable to contact resource.", e); + if (!avoids.add(e)) { + if (e.getScope() == Volume.class || e.getScope() == Nic.class) { + throw e; + } else { + s_logger.warn("unexpected ResourceUnavailableException : " + e.getScope().getName(), e); + throw e; + } + } + } catch (InsufficientCapacityException e) { + s_logger.info("Insufficient capacity ", e); + if (!avoids.add(e)) { + if (e.getScope() == Volume.class || e.getScope() == Nic.class) { + throw e; + } else { + s_logger.warn("unexpected InsufficientCapacityException : " + e.getScope().getName(), e); + } + } + } catch (Exception e) { + s_logger.error("Failed to start instance " + vm, e); + throw new AgentUnavailableException("Unable to start instance due to " + e.getMessage(), destHostId, e); + } finally { + if (startedVm == null && canRetry) { + Step prevStep = work.getStep(); + _workDao.updateStep(work, Step.Release); + if (prevStep == Step.Started || prevStep == Step.Starting) { + cleanup(vmGuru, vmProfile, work, Event.OperationFailed, false, caller, account); + } else { + //if step is not starting/started, send cleanup command with force=true + cleanup(vmGuru, vmProfile, work, Event.OperationFailed, true, caller, account); + } + } + } + } + } finally { + if (startedVm == null) { + if (canRetry) { + try { + changeState(vm, Event.OperationFailed, null, work, Step.Done); + } catch (NoTransitionException e) { + throw new ConcurrentOperationException(e.getMessage()); + } + } + } + } + + return startedVm; + } + + @Override + public boolean stop(T vm, User user, Account account) throws ResourceUnavailableException { + try { + return advanceStop(vm, false, user, account); + } catch (OperationTimedoutException e) { + throw new AgentUnavailableException("Unable to stop vm because the operation to stop timed out", vm.getHostId(), e); + } catch (ConcurrentOperationException e) { + throw new CloudRuntimeException("Unable to stop vm because of a concurrent operation", e); + } + } + + protected boolean sendStop(VirtualMachineGuru guru, VirtualMachineProfile profile, boolean force) { + VMInstanceVO vm = profile.getVirtualMachine(); + StopCommand stop = new StopCommand(vm, vm.getInstanceName(), null); + try { + Answer answer = _agentMgr.send(vm.getHostId(), stop); + if (!answer.getResult()) { + s_logger.debug("Unable to stop VM due to " + answer.getDetails()); + return false; + } + + guru.finalizeStop(profile, (StopAnswer) answer); + } catch (AgentUnavailableException e) { + if (!force) { + return false; + } + } catch (OperationTimedoutException e) { + if (!force) { + return false; + } + } + + return true; + } + + protected boolean cleanup(VirtualMachineGuru guru, VirtualMachineProfile profile, ItWorkVO work, Event event, boolean force, User user, Account account) { + T vm = profile.getVirtualMachine(); + State state = vm.getState(); + s_logger.debug("Cleaning up resources for the vm " + vm + " in " + state + " state"); + if (state == State.Starting) { + Step step = work.getStep(); + if (step == Step.Starting && !force) { + s_logger.warn("Unable to cleanup vm " + vm + "; work state is incorrect: " + step); + return false; + } + + if (step == Step.Started || step == Step.Starting || step == Step.Release) { + if (vm.getHostId() != null) { + if (!sendStop(guru, profile, force)) { + s_logger.warn("Failed to stop vm " + vm + " in " + State.Starting + " state as a part of cleanup process"); + return false; + } + } + } + + if (step != Step.Release && step != Step.Prepare && step != Step.Started && step != Step.Starting) { + s_logger.debug("Cleanup is not needed for vm " + vm + "; work state is incorrect: " + step); + return true; + } + } else if (state == State.Stopping) { + if (vm.getHostId() != null) { + if (!sendStop(guru, profile, force)) { + s_logger.warn("Failed to stop vm " + vm + " in " + State.Stopping + " state as a part of cleanup process"); + return false; + } + } + } else if (state == State.Migrating) { + if (vm.getHostId() != null) { + if (!sendStop(guru, profile, force)) { + s_logger.warn("Failed to stop vm " + vm + " in " + State.Migrating + " state as a part of cleanup process"); + return false; + } + } + if (vm.getLastHostId() != null) { + if (!sendStop(guru, profile, force)) { + s_logger.warn("Failed to stop vm " + vm + " in " + State.Migrating + " state as a part of cleanup process"); + return false; + } + } + } else if (state == State.Running) { + if (!sendStop(guru, profile, force)) { + s_logger.warn("Failed to stop vm " + vm + " in " + State.Running + " state as a part of cleanup process"); + return false; + } + } + + try { + _networkMgr.release(profile, force); + s_logger.debug("Successfully released network resources for the vm " + vm); + } catch (Exception e) { + s_logger.warn("Unable to release some network resources.", e); + } + + _storageMgr.release(profile); + s_logger.debug("Successfully cleanued up resources for the vm " + vm + " in " + state + " state"); + return true; + } + + @Override + public boolean advanceStop(T vm, boolean forced, User user, Account account) throws AgentUnavailableException, OperationTimedoutException, ConcurrentOperationException { + State state = vm.getState(); + if (state == State.Stopped) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("VM is already stopped: " + vm); + } + return true; + } + + if (state == State.Destroyed || state == State.Expunging || state == State.Error) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Stopped called on " + vm + " but the state is " + state); + } + return true; + } + // grab outstanding work item if any + ItWorkVO work = _workDao.findByOutstandingWork(vm.getId(), vm.getState()); + if (work != null) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Found an outstanding work item for this vm " + vm + " with state:" + vm.getState() + ", work id:" + work.getId()); + } + } + Long hostId = vm.getHostId(); + if (hostId == null) { + if (!forced) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("HostId is null but this is not a forced stop, cannot stop vm " + vm + " with state:" + vm.getState()); + } + return false; + } + try { + stateTransitTo(vm, Event.AgentReportStopped, null, null); + } catch (NoTransitionException e) { + s_logger.warn(e.getMessage()); + } + // mark outstanding work item if any as done + if (work != null) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Updating work item to Done, id:" + work.getId()); + } + work.setStep(Step.Done); + _workDao.update(work.getId(), work); + } + return true; + } + + VirtualMachineGuru vmGuru = getVmGuru(vm); + VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm); + + try { + if (!stateTransitTo(vm, Event.StopRequested, vm.getHostId())) { + throw new ConcurrentOperationException("VM is being operated on."); + } + } catch (NoTransitionException e1) { + if (!forced) { + throw new CloudRuntimeException("We cannot stop " + vm + " when it is in state " + vm.getState()); + } + boolean doCleanup = false; + if (s_logger.isDebugEnabled()) { + s_logger.debug("Unable to transition the state but we're moving on because it's forced stop"); + } + if (state == State.Starting || state == State.Migrating) { + if (work != null) { + doCleanup = true; + } else { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Unable to cleanup VM: " + vm + " ,since outstanding work item is not found"); + } + throw new CloudRuntimeException("Work item not found, We cannot stop " + vm + " when it is in state " + vm.getState()); + } + } else if (state == State.Stopping) { + doCleanup = true; + } + + if (doCleanup) { + if (cleanup(vmGuru, new VirtualMachineProfileImpl(vm), work, Event.StopRequested, forced, user, account)) { + try { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Updating work item to Done, id:" + work.getId()); + } + return changeState(vm, Event.AgentReportStopped, null, work, Step.Done); + } catch (NoTransitionException e) { + s_logger.warn("Unable to cleanup " + vm); + return false; + } + } else { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Failed to cleanup VM: " + vm); + } + throw new CloudRuntimeException("Failed to cleanup " + vm + " , current state " + vm.getState()); + } + } + } + + if (vm.getState() != State.Stopping) { + throw new CloudRuntimeException("We cannot proceed with stop VM " + vm + " since it is not in 'Stopping' state, current state: " + vm.getState()); + } + + vmGuru.prepareStop(profile); + + StopCommand stop = new StopCommand(vm, vm.getInstanceName(), null); + boolean stopped = false; + StopAnswer answer = null; + try { + answer = (StopAnswer) _agentMgr.send(vm.getHostId(), stop); + stopped = answer.getResult(); + if (!stopped) { + throw new CloudRuntimeException("Unable to stop the virtual machine due to " + answer.getDetails()); + } + vmGuru.finalizeStop(profile, answer); + + } catch (AgentUnavailableException e) { + } catch (OperationTimedoutException e) { + } finally { + if (!stopped) { + if (!forced) { + s_logger.warn("Unable to stop vm " + vm); + try { + stateTransitTo(vm, Event.OperationFailed, vm.getHostId()); + } catch (NoTransitionException e) { + s_logger.warn("Unable to transition the state " + vm); + } + return false; + } else { + s_logger.warn("Unable to actually stop " + vm + " but continue with release because it's a force stop"); + vmGuru.finalizeStop(profile, answer); + } + } + } + + if (s_logger.isDebugEnabled()) { + s_logger.debug(vm + " is stopped on the host. Proceeding to release resource held."); + } + + try { + _networkMgr.release(profile, forced); + s_logger.debug("Successfully released network resources for the vm " + vm); + } catch (Exception e) { + s_logger.warn("Unable to release some network resources.", e); + } + + try { + if (vm.getHypervisorType() != HypervisorType.BareMetal) { + _storageMgr.release(profile); + s_logger.debug("Successfully released storage resources for the vm " + vm); + } + } catch (Exception e) { + s_logger.warn("Unable to release storage resources.", e); + } + + try { + if (work != null) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Updating the outstanding work item to Done, id:" + work.getId()); + } + work.setStep(Step.Done); + _workDao.update(work.getId(), work); + } + + return stateTransitTo(vm, Event.OperationSucceeded, null, null); + } catch (NoTransitionException e) { + s_logger.warn(e.getMessage()); + return false; + } + } + + private void setStateMachine() { + _stateMachine = VirtualMachine.State.getStateMachine(); + } + + protected boolean stateTransitTo(VMInstanceVO vm, VirtualMachine.Event e, Long hostId, String reservationId) throws NoTransitionException { + vm.setReservationId(reservationId); + return _stateMachine.transitTo(vm, e, new Pair(vm.getHostId(), hostId), _vmDao); + } + + @Override + public boolean stateTransitTo(VMInstanceVO vm, VirtualMachine.Event e, Long hostId) throws NoTransitionException { + State oldState = vm.getState(); + if (oldState == State.Starting) { + if (e == Event.OperationSucceeded) { + vm.setLastHostId(hostId); + } + } else if (oldState == State.Stopping) { + if (e == Event.OperationSucceeded) { + vm.setLastHostId(vm.getHostId()); + } + } + return _stateMachine.transitTo(vm, e, new Pair(vm.getHostId(), hostId), _vmDao); + } + + @Override + public boolean remove(T vm, User user, Account caller) { + return _vmDao.remove(vm.getId()); + } + + @Override + public boolean destroy(T vm, User user, Account caller) throws AgentUnavailableException, OperationTimedoutException, ConcurrentOperationException { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Destroying vm " + vm); + } + if (vm == null || vm.getState() == State.Destroyed || vm.getState() == State.Expunging || vm.getRemoved() != null) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Unable to find vm or vm is destroyed: " + vm); + } + return true; + } + + if (!advanceStop(vm, _forceStop, user, caller)) { + s_logger.debug("Unable to stop " + vm); + return false; + } + + try { + if (!stateTransitTo(vm, VirtualMachine.Event.DestroyRequested, vm.getHostId())) { + s_logger.debug("Unable to destroy the vm because it is not in the correct state: " + vm); + return false; + } + } catch (NoTransitionException e) { + s_logger.debug(e.getMessage()); + return false; + } + + return true; + } + + protected boolean checkVmOnHost(VirtualMachine vm, long hostId) throws AgentUnavailableException, OperationTimedoutException { + CheckVirtualMachineAnswer answer = (CheckVirtualMachineAnswer) _agentMgr.send(hostId, new CheckVirtualMachineCommand(vm.getInstanceName())); + if (!answer.getResult() || answer.getState() == State.Stopped) { + return false; + } + + return true; + } + + @Override + public T storageMigration(T vm, StoragePool destPool) { + VirtualMachineGuru vmGuru = getVmGuru(vm); + + long vmId = vm.getId(); + vm = vmGuru.findById(vmId); + + try { + stateTransitTo(vm, VirtualMachine.Event.StorageMigrationRequested, null); + } catch (NoTransitionException e) { + s_logger.debug("Unable to migrate vm: " + e.toString()); + throw new CloudRuntimeException("Unable to migrate vm: " + e.toString()); + } + + VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm); + boolean migrationResult = false; + try { + migrationResult = _storageMgr.StorageMigration(profile, destPool); + + if (migrationResult) { + //if the vm is migrated to different pod in basic mode, need to reallocate ip + + if (vm.getPodIdToDeployIn() != destPool.getPodId()) { + DataCenterDeployment plan = new DataCenterDeployment(vm.getDataCenterIdToDeployIn(), destPool.getPodId(), null, null, null, null); + VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vm, null, null, null, null); + _networkMgr.reallocate(vmProfile, plan); + } + + //when start the vm next time, don;'t look at last_host_id, only choose the host based on volume/storage pool + vm.setLastHostId(null); + vm.setPodId(destPool.getPodId()); + } else { + s_logger.debug("Storage migration failed"); + } + } catch (ConcurrentOperationException e) { + s_logger.debug("Failed to migration: " + e.toString()); + throw new CloudRuntimeException("Failed to migration: " + e.toString()); + } catch (InsufficientVirtualNetworkCapcityException e) { + s_logger.debug("Failed to migration: " + e.toString()); + throw new CloudRuntimeException("Failed to migration: " + e.toString()); + } catch (InsufficientAddressCapacityException e) { + s_logger.debug("Failed to migration: " + e.toString()); + throw new CloudRuntimeException("Failed to migration: " + e.toString()); + } catch (InsufficientCapacityException e) { + s_logger.debug("Failed to migration: " + e.toString()); + throw new CloudRuntimeException("Failed to migration: " + e.toString()); + } finally { + try { + stateTransitTo(vm, VirtualMachine.Event.AgentReportStopped, null); + } catch (NoTransitionException e) { + s_logger.debug("Failed to change vm state: " + e.toString()); + throw new CloudRuntimeException("Failed to change vm state: " + e.toString()); + } + } + + return vm; + } + + @Override + public T migrate(T vm, long srcHostId, DeployDestination dest) throws ResourceUnavailableException, ConcurrentOperationException, ManagementServerException, + VirtualMachineMigrationException { + s_logger.info("Migrating " + vm + " to " + dest); + + long dstHostId = dest.getHost().getId(); + Host fromHost = _hostDao.findById(srcHostId); + if (fromHost == null) { + s_logger.info("Unable to find the host to migrate from: " + srcHostId); + throw new CloudRuntimeException("Unable to find the host to migrate from: " + srcHostId); + } + + if (fromHost.getClusterId().longValue() != dest.getCluster().getId()) { + s_logger.info("Source and destination host are not in same cluster, unable to migrate to host: " + dest.getHost().getId()); + throw new CloudRuntimeException("Source and destination host are not in same cluster, unable to migrate to host: " + dest.getHost().getId()); + } + + VirtualMachineGuru vmGuru = getVmGuru(vm); + + long vmId = vm.getId(); + vm = vmGuru.findById(vmId); + if (vm == null) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Unable to find the vm " + vm); + } + throw new ManagementServerException("Unable to find a virtual machine with id " + vmId); + } + + if (vm.getState() != State.Running) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("VM is not Running, unable to migrate the vm " + vm); + } + throw new VirtualMachineMigrationException("VM is not Running, unable to migrate the vm currently " + vm + " , current state: " + vm.getState().toString()); + } + + short alertType = AlertManager.ALERT_TYPE_USERVM_MIGRATE; + if (VirtualMachine.Type.DomainRouter.equals(vm.getType())) { + alertType = AlertManager.ALERT_TYPE_DOMAIN_ROUTER_MIGRATE; + } else if (VirtualMachine.Type.ConsoleProxy.equals(vm.getType())) { + alertType = AlertManager.ALERT_TYPE_CONSOLE_PROXY_MIGRATE; + } + + VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm); + _networkMgr.prepareNicForMigration(profile, dest); + _storageMgr.prepareForMigration(profile, dest); + + VirtualMachineTO to = toVmTO(profile); + PrepareForMigrationCommand pfmc = new PrepareForMigrationCommand(to); + + ItWorkVO work = new ItWorkVO(UUID.randomUUID().toString(), _nodeId, State.Migrating, vm.getType(), vm.getId()); + work.setStep(Step.Prepare); + work.setResourceType(ItWorkVO.ResourceType.Host); + work.setResourceId(dstHostId); + work = _workDao.persist(work); + + PrepareForMigrationAnswer pfma = null; + try { + pfma = (PrepareForMigrationAnswer) _agentMgr.send(dstHostId, pfmc); + if (!pfma.getResult()) { + String msg = "Unable to prepare for migration due to " + pfma.getDetails(); + pfma = null; + throw new AgentUnavailableException(msg, dstHostId); + } + } catch (OperationTimedoutException e1) { + throw new AgentUnavailableException("Operation timed out", dstHostId); + } finally { + if (pfma == null) { + work.setStep(Step.Done); + _workDao.update(work.getId(), work); + } + } + + vm.setLastHostId(srcHostId); + try { + if (vm == null || vm.getHostId() == null || vm.getHostId() != srcHostId || !changeState(vm, Event.MigrationRequested, dstHostId, work, Step.Migrating)) { + s_logger.info("Migration cancelled because state has changed: " + vm); + throw new ConcurrentOperationException("Migration cancelled because state has changed: " + vm); + } + } catch (NoTransitionException e1) { + s_logger.info("Migration cancelled because " + e1.getMessage()); + throw new ConcurrentOperationException("Migration cancelled because " + e1.getMessage()); + } + + boolean migrated = false; + try { + boolean isWindows = _guestOsCategoryDao.findById(_guestOsDao.findById(vm.getGuestOSId()).getCategoryId()).getName().equalsIgnoreCase("Windows"); + MigrateCommand mc = new MigrateCommand(vm.getInstanceName(), dest.getHost().getPrivateIpAddress(), isWindows); + mc.setHostGuid(dest.getHost().getGuid()); + + try { + MigrateAnswer ma = (MigrateAnswer) _agentMgr.send(vm.getLastHostId(), mc); + if (!ma.getResult()) { + s_logger.error("Unable to migrate due to " + ma.getDetails()); + return null; + } + } catch (OperationTimedoutException e) { + if (e.isActive()) { + s_logger.warn("Active migration command so scheduling a restart for " + vm); + _haMgr.scheduleRestart(vm, true); + } + throw new AgentUnavailableException("Operation timed out on migrating " + vm, dstHostId); + } + + try { + if (!changeState(vm, VirtualMachine.Event.OperationSucceeded, dstHostId, work, Step.Started)) { + throw new ConcurrentOperationException("Unable to change the state for " + vm); + } + } catch (NoTransitionException e1) { + throw new ConcurrentOperationException("Unable to change state due to " + e1.getMessage()); + } + + try { + if (!checkVmOnHost(vm, dstHostId)) { + s_logger.error("Unable to complete migration for " + vm); + try { + _agentMgr.send(srcHostId, new Commands(cleanup(vm.getInstanceName())), null); + } catch (AgentUnavailableException e) { + s_logger.error("AgentUnavailableException while cleanup on source host: " + srcHostId); + } + cleanup(vmGuru, new VirtualMachineProfileImpl(vm), work, Event.AgentReportStopped, true, _accountMgr.getSystemUser(), _accountMgr.getSystemAccount()); + return null; + } + } catch (OperationTimedoutException e) { + } + + migrated = true; + return vm; + } finally { + if (!migrated) { + s_logger.info("Migration was unsuccessful. Cleaning up: " + vm); + + _alertMgr.sendAlert(alertType, fromHost.getDataCenterId(), fromHost.getPodId(), "Unable to migrate vm " + vm.getInstanceName() + " from host " + fromHost.getName() + " in zone " + + dest.getDataCenter().getName() + " and pod " + dest.getPod().getName(), "Migrate Command failed. Please check logs."); + try { + _agentMgr.send(dstHostId, new Commands(cleanup(vm.getInstanceName())), null); + } catch (AgentUnavailableException ae) { + s_logger.info("Looks like the destination Host is unavailable for cleanup"); + } + + try { + stateTransitTo(vm, Event.OperationFailed, srcHostId); + } catch (NoTransitionException e) { + s_logger.warn(e.getMessage()); + } + } + + work.setStep(Step.Done); + _workDao.update(work.getId(), work); + } + } + + @Override + public VirtualMachineTO toVmTO(VirtualMachineProfile profile) { + HypervisorGuru hvGuru = _hvGuruMgr.getGuru(profile.getVirtualMachine().getHypervisorType()); + VirtualMachineTO to = hvGuru.implement(profile); + return to; + } + + protected void cancelWorkItems(long nodeId) { + GlobalLock scanLock = GlobalLock.getInternLock("vmmgr.cancel.workitem"); + + try { + if (scanLock.lock(3)) { + try { + List works = _workDao.listWorkInProgressFor(nodeId); + for (ItWorkVO work : works) { + s_logger.info("Handling unfinished work item: " + work); + try { + VMInstanceVO vm = _vmDao.findById(work.getInstanceId()); + if (vm != null) { + if (work.getType() == State.Starting) { + _haMgr.scheduleRestart(vm, true); + work.setManagementServerId(_nodeId); + _workDao.update(work.getId(), work); + } else if (work.getType() == State.Stopping) { + _haMgr.scheduleStop(vm, vm.getHostId(), WorkType.CheckStop); + work.setManagementServerId(_nodeId); + _workDao.update(work.getId(), work); + } else if (work.getType() == State.Migrating) { + _haMgr.scheduleMigration(vm); + work.setStep(Step.Done); + _workDao.update(work.getId(), work); + } + } + } catch (Exception e) { + s_logger.error("Error while handling " + work, e); + } + } + } finally { + scanLock.unlock(); + } + } + } finally { + scanLock.releaseRef(); + } + } + + @Override + public boolean migrateAway(VirtualMachine.Type vmType, long vmId, long srcHostId) throws InsufficientServerCapacityException, VirtualMachineMigrationException { + VirtualMachineGuru vmGuru = _vmGurus.get(vmType); + VMInstanceVO vm = vmGuru.findById(vmId); + if (vm == null) { + s_logger.debug("Unable to find a VM for " + vmId); + return true; + } + + VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm); + + Long hostId = vm.getHostId(); + if (hostId == null) { + s_logger.debug("Unable to migrate because the VM doesn't have a host id: " + vm); + return true; + } + + Host host = _hostDao.findById(hostId); + + DataCenterDeployment plan = new DataCenterDeployment(host.getDataCenterId(), host.getPodId(), host.getClusterId(), null, null, null); + ExcludeList excludes = new ExcludeList(); + excludes.addHost(hostId); + + DeployDestination dest = null; + while (true) { + for (DeploymentPlanner planner : _planners) { + if (planner.canHandle(profile, plan, excludes)) { + dest = planner.plan(profile, plan, excludes); + } else { + continue; + } + + if (dest != null) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Planner " + planner + " found " + dest + " for migrating to."); + } + break; + } + if (s_logger.isDebugEnabled()) { + s_logger.debug("Planner " + planner + " was unable to find anything."); + } + } + + if (dest == null) { + throw new InsufficientServerCapacityException("Unable to find a server to migrate to.", host.getClusterId()); + } + + excludes.addHost(dest.getHost().getId()); + VMInstanceVO vmInstance = null; + try { + vmInstance = migrate(vm, srcHostId, dest); + } catch (ResourceUnavailableException e) { + s_logger.debug("Unable to migrate to unavailable " + dest); + } catch (ConcurrentOperationException e) { + s_logger.debug("Unable to migrate VM due to: " + e.getMessage()); + } catch (ManagementServerException e) { + s_logger.debug("Unable to migrate VM: " + e.getMessage()); + } catch (VirtualMachineMigrationException e) { + s_logger.debug("Got VirtualMachineMigrationException, Unable to migrate: " + e.getMessage()); + if (vm.getState() == State.Starting) { + s_logger.debug("VM seems to be still Starting, we should retry migration later"); + throw e; + } else { + s_logger.debug("Unable to migrate VM, VM is not in Running or even Starting state, current state: " + vm.getState().toString()); + } + } + if (vmInstance != null) { + return true; + } + try { + boolean result = advanceStop(vm, true, _accountMgr.getSystemUser(), _accountMgr.getSystemAccount()); + return result; + } catch (ResourceUnavailableException e) { + s_logger.debug("Unable to stop VM due to " + e.getMessage()); + } catch (ConcurrentOperationException e) { + s_logger.debug("Unable to stop VM due to " + e.getMessage()); + } catch (OperationTimedoutException e) { + s_logger.debug("Unable to stop VM due to " + e.getMessage()); + } + return false; + } + } + + protected class CleanupTask implements Runnable { + @Override + public void run() { + s_logger.trace("VM Operation Thread Running"); + try { + _workDao.cleanup(_cleanupWait); + } catch (Exception e) { + s_logger.error("VM Operations failed due to ", e); + } + } + } + + @Override + public boolean isVirtualMachineUpgradable(VirtualMachine vm, ServiceOffering offering) { + Enumeration en = _hostAllocators.enumeration(); + boolean isMachineUpgradable = true; + while (isMachineUpgradable && en.hasMoreElements()) { + final HostAllocator allocator = en.nextElement(); + isMachineUpgradable = allocator.isVirtualMachineUpgradable(vm, offering); + } + + return isMachineUpgradable; + } + + @Override + public T reboot(T vm, Map params, User caller, Account account) throws InsufficientCapacityException, ResourceUnavailableException { + try { + return advanceReboot(vm, params, caller, account); + } catch (ConcurrentOperationException e) { + throw new CloudRuntimeException("Unable to reboot a VM due to concurrent operation", e); + } + } + + @Override + public T advanceReboot(T vm, Map params, User caller, Account account) throws InsufficientCapacityException, + ConcurrentOperationException, ResourceUnavailableException { + T rebootedVm = null; + + DataCenter dc = _configMgr.getZone(vm.getDataCenterIdToDeployIn()); + Host host = _hostDao.findById(vm.getHostId()); + Cluster cluster = null; + if (host != null) { + cluster = _configMgr.getCluster(host.getClusterId()); + } + HostPodVO pod = _configMgr.getPod(host.getPodId()); + DeployDestination dest = new DeployDestination(dc, pod, cluster, host); + + try { + + Commands cmds = new Commands(OnError.Stop); + cmds.addCommand(new RebootCommand(vm.getInstanceName())); + _agentMgr.send(host.getId(), cmds); + + Answer rebootAnswer = cmds.getAnswer(RebootAnswer.class); + if (rebootAnswer != null && rebootAnswer.getResult()) { + rebootedVm = vm; + return rebootedVm; + } + s_logger.info("Unable to reboot VM " + vm + " on " + dest.getHost() + " due to " + (rebootAnswer == null ? " no reboot answer" : rebootAnswer.getDetails())); + } catch (OperationTimedoutException e) { + s_logger.warn("Unable to send the reboot command to host " + dest.getHost() + " for the vm " + vm + " due to operation timeout", e); + throw new CloudRuntimeException("Failed to reboot the vm on host " + dest.getHost()); + } + + return rebootedVm; + } + + @Override + public VMInstanceVO findByIdAndType(VirtualMachine.Type type, long vmId) { + VirtualMachineGuru guru = _vmGurus.get(type); + return guru.findById(vmId); + } + + public Command cleanup(String vmName) { + return new StopCommand(vmName); + } + + public Commands fullHostSync(final long hostId, StartupRoutingCommand startup) { + Commands commands = new Commands(OnError.Continue); + + Map infos = convertToInfos(startup); + + final List vms = _vmDao.listByHostId(hostId); + s_logger.debug("Found " + vms.size() + " VMs for host " + hostId); + for (VMInstanceVO vm : vms) { + AgentVmInfo info = infos.remove(vm.getId()); + VMInstanceVO castedVm = null; + if (info == null) { + info = new AgentVmInfo(vm.getInstanceName(), getVmGuru(vm), vm, State.Stopped); + } + castedVm = info.guru.findById(vm.getId()); + + HypervisorGuru hvGuru = _hvGuruMgr.getGuru(castedVm.getHypervisorType()); + Command command = compareState(hostId, castedVm, info, true, hvGuru.trackVmHostChange()); + if (command != null) { + commands.addCommand(command); + } + } + + for (final AgentVmInfo left : infos.values()) { + boolean found = false; + for (VirtualMachineGuru vmGuru : _vmGurus.values()) { + VMInstanceVO vm = vmGuru.findByName(left.name); + if (vm != null) { + found = true; + HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vm.getHypervisorType()); + if(hvGuru.trackVmHostChange()) { + Command command = compareState(hostId, vm, left, true, true); + if (command != null) { + commands.addCommand(command); + } + } else { + s_logger.warn("Stopping a VM, VM " + left.name + " migrate from Host " + vm.getHostId() + " to Host " + hostId ); + commands.addCommand(cleanup(left.name)); + } + break; + } + } + if ( ! found ) { + s_logger.warn("Stopping a VM that we have no record of : " + left.name); + commands.addCommand(cleanup(left.name)); + } + } + + return commands; + } + + public Commands deltaHostSync(long hostId, Map newStates) { + Map states = convertDeltaToInfos(newStates); + Commands commands = new Commands(OnError.Continue); + + for (Map.Entry entry : states.entrySet()) { + AgentVmInfo info = entry.getValue(); + + VMInstanceVO vm = info.vm; + + Command command = null; + if (vm != null) { + HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vm.getHypervisorType()); + command = compareState(hostId, vm, info, false, hvGuru.trackVmHostChange()); + } else { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Cleaning up a VM that is no longer found: " + info.name); + } + command = cleanup(info.name); + } + + if (command != null) { + commands.addCommand(command); + } + } + + return commands; + } + + + + public void deltaSync(Map> newStates) { + Map states = convertToInfos(newStates); + + for (Map.Entry entry : states.entrySet()) { + AgentVmInfo info = entry.getValue(); + VMInstanceVO vm = info.vm; + Command command = null; + if (vm != null) { + Host host = _resourceMgr.findHostByGuid(info.getHostUuid()); + long hId = host.getId(); + + HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vm.getHypervisorType()); + command = compareState(hId, vm, info, false, hvGuru.trackVmHostChange()); + } else { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Cleaning up a VM that is no longer found : " + info.name); + } + command = cleanup(info.name); + } + if (command != null){ + try { + Host host = _resourceMgr.findHostByGuid(info.getHostUuid()); + if (host != null){ + 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()); + } + } + } + } + + + public void fullSync(final long clusterId, Map> newStates) { + if (newStates==null)return; + Map infos = convertToInfos(newStates); + Set set_vms = Collections.synchronizedSet(new HashSet()); + set_vms.addAll(_vmDao.listByClusterId(clusterId)); + set_vms.addAll(_vmDao.listLHByClusterId(clusterId)); + + for (VMInstanceVO vm : set_vms) { + if (vm.isRemoved() || vm.getState() == State.Destroyed || vm.getState() == State.Expunging) continue; + 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))) + { + 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) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Found an outstanding work item for this vm " + vm + " in state:" + vm.getState() + ", work id:" + work.getId()); + } + } + vm.setState(State.Running); // set it as running and let HA take care of it + _vmDao.persist(vm); + + if (work != null) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Updating outstanding work item to Done, id:" + work.getId()); + } + work.setStep(Step.Done); + _workDao.update(work.getId(), work); + } + + castedVm = info.guru.findById(vm.getId()); + try { + Host host = _hostDao.findByGuid(info.getHostUuid()); + long hostId = host == null ? (vm.getHostId() == null ? vm.getLastHostId() : vm.getHostId()) : host.getId(); + HypervisorGuru hvGuru = _hvGuruMgr.getGuru(castedVm.getHypervisorType()); + Command command = compareState(hostId, castedVm, info, true, hvGuru.trackVmHostChange()); + if (command != null){ + Answer answer = _agentMgr.send(hostId, command); + if (!answer.getResult()) { + s_logger.warn("Failed to update state of the VM due to " + answer.getDetails()); + } + } + } catch (Exception e) { + s_logger.warn("Unable to update state of the VM due to exception " + e.getMessage()); + e.printStackTrace(); + } + } + else if (info != null && (vm.getState() == State.Stopped || vm.getState() == State.Stopping)) { + 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()); + } + } + } + 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 + 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()); + vm.setHostId(null); + _vmDao.persist(vm); + }*/ + } + + for (final AgentVmInfo left : infos.values()) { + 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){ + s_logger.warn("Stopping a VM which we do not have any record of " + left.name); + Answer answer = _agentMgr.send(host.getId(), cleanup(left.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()); + } + } + + } + + + + protected Map convertToInfos(final Map> newStates) { + final HashMap map = new HashMap(); + if (newStates == null) { + return map; + } + Collection> vmGurus = _vmGurus.values(); + boolean is_alien_vm = true; + long alien_vm_count = -1; + for (Map.Entry> entry : newStates.entrySet()) { + is_alien_vm = true; + for (VirtualMachineGuru vmGuru : vmGurus) { + String name = entry.getKey(); + VMInstanceVO vm = vmGuru.findByName(name); + if (vm != null) { + map.put(vm.getId(), new AgentVmInfo(entry.getKey(), vmGuru, vm, entry.getValue().second(), entry.getValue().first())); + is_alien_vm = false; + break; + } + Long id = vmGuru.convertToId(name); + if (id != null) { + map.put(id, new AgentVmInfo(entry.getKey(), vmGuru, null, entry.getValue().second(), entry.getValue().first())); + is_alien_vm = false; + break; + } + } + // 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()); + } + } + return map; + } + + protected Map convertToInfos(StartupRoutingCommand cmd) { + final Map states = cmd.getVmStates(); + final HashMap map = new HashMap(); + if (states == null) { + return map; + } + Collection> vmGurus = _vmGurus.values(); + + for (Map.Entry entry : states.entrySet()) { + for (VirtualMachineGuru vmGuru : vmGurus) { + String name = entry.getKey(); + VMInstanceVO vm = vmGuru.findByName(name); + if (vm != null) { + map.put(vm.getId(), new AgentVmInfo(entry.getKey(), vmGuru, vm, entry.getValue().getState(), entry.getValue().getHost() )); + break; + } + Long id = vmGuru.convertToId(name); + if (id != null) { + map.put(id, new AgentVmInfo(entry.getKey(), vmGuru, null,entry.getValue().getState(), entry.getValue().getHost() )); + break; + } + } + } + + return map; + } + + protected Map convertDeltaToInfos(final Map states) { + final HashMap map = new HashMap(); + + if (states == null) { + return map; + } + + Collection> vmGurus = _vmGurus.values(); + + for (Map.Entry entry : states.entrySet()) { + for (VirtualMachineGuru vmGuru : vmGurus) { + String name = entry.getKey(); + + VMInstanceVO vm = vmGuru.findByName(name); + + if (vm != null) { + map.put(vm.getId(), new AgentVmInfo(entry.getKey(), vmGuru, vm, entry.getValue())); + break; + } + + Long id = vmGuru.convertToId(name); + if (id != null) { + map.put(id, new AgentVmInfo(entry.getKey(), vmGuru, null,entry.getValue())); + break; + } + } + } + + return map; + } + + + + /** + * compareState does as its name suggests and compares the states between + * management server and agent. It returns whether something should be + * cleaned up + * + */ + protected Command compareState(long hostId, VMInstanceVO vm, final AgentVmInfo info, final boolean fullSync, boolean trackExternalChange) { + State agentState = info.state; + final String agentName = info.name; + final State serverState = vm.getState(); + final String serverName = vm.getInstanceName(); + + Command command = null; + s_logger.debug("VM " + serverName + ": cs state = " + serverState + " and realState = " + agentState); + if (s_logger.isDebugEnabled()) { + s_logger.debug("VM " + serverName + ": cs state = " + serverState + " and realState = " + agentState); + } + + if (agentState == State.Error) { + agentState = State.Stopped; + + short alertType = AlertManager.ALERT_TYPE_USERVM; + if (VirtualMachine.Type.DomainRouter.equals(vm.getType())) { + alertType = AlertManager.ALERT_TYPE_DOMAIN_ROUTER; + } else if (VirtualMachine.Type.ConsoleProxy.equals(vm.getType())) { + alertType = AlertManager.ALERT_TYPE_CONSOLE_PROXY; + } else if (VirtualMachine.Type.SecondaryStorageVm.equals(vm.getType())) { + alertType = AlertManager.ALERT_TYPE_SSVM; + } + + HostPodVO podVO = _podDao.findById(vm.getPodIdToDeployIn()); + DataCenterVO dcVO = _dcDao.findById(vm.getDataCenterIdToDeployIn()); + HostVO hostVO = _hostDao.findById(vm.getHostId()); + + String hostDesc = "name: " + hostVO.getName() + " (id:" + hostVO.getId() + "), availability zone: " + dcVO.getName() + ", pod: " + podVO.getName(); + _alertMgr.sendAlert(alertType, vm.getDataCenterIdToDeployIn(), vm.getPodIdToDeployIn(), "VM (name: " + vm.getInstanceName() + ", id: " + vm.getId() + ") stopped on host " + hostDesc + + " due to storage failure", "Virtual Machine " + vm.getInstanceName() + " (id: " + vm.getId() + ") running on host [" + vm.getHostId() + "] stopped due to storage failure."); + } + + if (trackExternalChange) { + if (serverState == State.Starting) { + if (vm.getHostId() != null && vm.getHostId() != hostId) { + s_logger.info("CloudStack is starting VM on host " + vm.getHostId() + ", but status report comes from a different host " + hostId + ", skip status sync for vm: " + + vm.getInstanceName()); + return null; + } + } + if (vm.getHostId() == null || hostId != vm.getHostId()) { + try { + stateTransitTo(vm, VirtualMachine.Event.AgentReportMigrated, hostId); + } catch (NoTransitionException e) { + } + } + } + + // during VM migration time, don't sync state will agent status update + if (serverState == State.Migrating) { + s_logger.debug("Skipping vm in migrating state: " + vm); + return null; + } + + if (trackExternalChange) { + if (serverState == State.Starting) { + if (vm.getHostId() != null && vm.getHostId() != hostId) { + s_logger.info("CloudStack is starting VM on host " + vm.getHostId() + ", but status report comes from a different host " + hostId + ", skip status sync for vm: " + + vm.getInstanceName()); + return null; + } + } + + if (serverState == State.Running) { + try { + // + // we had a bug that sometimes VM may be at Running State + // but host_id is null, we will cover it here. + // means that when CloudStack DB lost of host information, + // we will heal it with the info reported from host + // + if (vm.getHostId() == null || hostId != vm.getHostId()) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("detected host change when VM " + vm + " is at running state, VM could be live-migrated externally from host " + vm.getHostId() + " to host " + hostId); + } + + stateTransitTo(vm, VirtualMachine.Event.AgentReportMigrated, hostId); + } + } catch (NoTransitionException e) { + s_logger.warn(e.getMessage()); + } + } + } + + if (agentState == serverState) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Both states are " + agentState + " for " + vm); + } + assert (agentState == State.Stopped || agentState == State.Running) : "If the states we send up is changed, this must be changed."; + if (agentState == State.Running) { + try { + stateTransitTo(vm, VirtualMachine.Event.AgentReportRunning, hostId); + } catch (NoTransitionException e) { + s_logger.warn(e.getMessage()); + } + // FIXME: What if someone comes in and sets it to stopping? Then + // what? + return null; + } + + s_logger.debug("State matches but the agent said stopped so let's send a cleanup command anyways."); + return cleanup(agentName); + } + + if (agentState == State.Shutdowned) { + if (serverState == State.Running || serverState == State.Starting || serverState == State.Stopping) { + try { + advanceStop(vm, true, _accountMgr.getSystemUser(), _accountMgr.getSystemAccount()); + } catch (AgentUnavailableException e) { + assert (false) : "How do we hit this with forced on?"; + return null; + } catch (OperationTimedoutException e) { + assert (false) : "How do we hit this with forced on?"; + return null; + } catch (ConcurrentOperationException e) { + assert (false) : "How do we hit this with forced on?"; + return null; + } + } else { + s_logger.debug("Sending cleanup to a shutdowned vm: " + agentName); + command = cleanup(agentName); + } + } else if (agentState == State.Stopped) { + // This state means the VM on the agent was detected previously + // and now is gone. This is slightly different than if the VM + // was never completed but we still send down a Stop Command + // to ensure there's cleanup. + if (serverState == State.Running) { + // Our records showed that it should be running so let's restart + // it. + _haMgr.scheduleRestart(vm, false); + } else if (serverState == State.Stopping) { + _haMgr.scheduleStop(vm, hostId, WorkType.ForceStop); + s_logger.debug("Scheduling a check stop for VM in stopping mode: " + vm); + } else if (serverState == State.Starting) { + s_logger.debug("Ignoring VM in starting mode: " + vm.getInstanceName()); + _haMgr.scheduleRestart(vm, false); + } + command = cleanup(agentName); + } else if (agentState == State.Running) { + if (serverState == State.Starting) { + if (fullSync) { + try { + ensureVmRunningContext(hostId, vm, Event.AgentReportRunning); + } catch (OperationTimedoutException e) { + s_logger.error("Exception during update for running vm: " + vm, e); + return null; + } catch (ResourceUnavailableException e) { + s_logger.error("Exception during update for running vm: " + vm, e); + return null; + }catch (InsufficientAddressCapacityException e) { + s_logger.error("Exception during update for running vm: " + vm, e); + return null; + }catch (NoTransitionException e) { + s_logger.warn(e.getMessage()); + } + } + } else if (serverState == State.Stopping) { + s_logger.debug("Scheduling a stop command for " + vm); + _haMgr.scheduleStop(vm, hostId, WorkType.Stop); + } else { + s_logger.debug("server VM state " + serverState + " does not meet expectation of a running VM report from agent"); + + // just be careful not to stop VM for things we don't handle + // command = cleanup(agentName); + } + } + return command; + } + + private void ensureVmRunningContext(long hostId, VMInstanceVO vm, Event cause) throws OperationTimedoutException, ResourceUnavailableException, NoTransitionException, InsufficientAddressCapacityException { + VirtualMachineGuru vmGuru = getVmGuru(vm); + + s_logger.debug("VM state is starting on full sync so updating it to running"); + vm = findByIdAndType(vm.getType(), vm.getId()); + + // grab outstanding work item if any + ItWorkVO work = _workDao.findByOutstandingWork(vm.getId(), vm.getState()); + if (work != null) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Found an outstanding work item for this vm " + vm + " in state:" + vm.getState() + ", work id:" + work.getId()); + } + } + + try { + stateTransitTo(vm, cause, hostId); + } catch (NoTransitionException e1) { + s_logger.warn(e1.getMessage()); + } + + s_logger.debug("VM's " + vm + " state is starting on full sync so updating it to Running"); + vm = vmGuru.findById(vm.getId()); // this should ensure vm has the most + // up to date info + + VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm); + List nics = _nicsDao.listByVmId(profile.getId()); + for (NicVO nic : nics) { + Network network = _networkMgr.getNetwork(nic.getNetworkId()); + NicProfile nicProfile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), null, + _networkMgr.isSecurityGroupSupportedInNetwork(network), _networkMgr.getNetworkTag(profile.getHypervisorType(), network)); + profile.addNic(nicProfile); + } + + Commands cmds = new Commands(OnError.Stop); + s_logger.debug("Finalizing commands that need to be send to complete Start process for the vm " + vm); + + if (vmGuru.finalizeCommandsOnStart(cmds, profile)) { + if (cmds.size() != 0) { + _agentMgr.send(vm.getHostId(), cmds); + } + + if (vmGuru.finalizeStart(profile, vm.getHostId(), cmds, null)) { + stateTransitTo(vm, cause, vm.getHostId()); + } else { + s_logger.error("Unable to finish finialization for running vm: " + vm); + } + } else { + s_logger.error("Unable to finalize commands on start for vm: " + vm); + } + + if (work != null) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Updating outstanding work item to Done, id:" + work.getId()); + } + work.setStep(Step.Done); + _workDao.update(work.getId(), work); + } + } + + @Override + public boolean isRecurring() { + return true; + } + + @Override + public boolean processAnswers(long agentId, long seq, Answer[] answers) { + for (final Answer answer : answers) { + if (answer instanceof ClusterSyncAnswer) { + ClusterSyncAnswer hs = (ClusterSyncAnswer) answer; + if (!hs.isExceuted()){ + deltaSync(hs.getNewStates()); + hs.setExecuted(); + } + } + } + return true; + } + + @Override + public boolean processTimeout(long agentId, long seq) { + return true; + } + + @Override + public int getTimeout() { + return -1; + } + + @Override + public boolean processCommands(long agentId, long seq, Command[] cmds) { + boolean processed = false; + for (Command cmd : cmds) { + if (cmd instanceof PingRoutingCommand) { + PingRoutingCommand ping = (PingRoutingCommand) cmd; + if (ping.getNewStates() != null && ping.getNewStates().size() > 0) { + Commands commands = deltaHostSync(agentId, ping.getNewStates()); + if (commands.size() > 0) { + try { + _agentMgr.send(agentId, commands, this); + } catch (final AgentUnavailableException e) { + s_logger.warn("Agent is now unavailable", e); + } + } + } + processed = true; + } + } + return processed; + } + + @Override + public AgentControlAnswer processControlCommand(long agentId, AgentControlCommand cmd) { + return null; + } + + @Override + public boolean processDisconnect(long agentId, Status state) { + return true; + } + + @Override + public void processConnect(HostVO agent, StartupCommand cmd, boolean forRebalance) throws ConnectionException { + if (!(cmd instanceof StartupRoutingCommand)) { + return; + } + + if (forRebalance) { + s_logger.debug("Not processing listener " + this + " as connect happens on rebalance process"); + return; + } + + if (forRebalance) { + s_logger.debug("Not processing listener " + this + " as connect happens on rebalance process"); + return; + } + + 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 + ClusterSyncCommand syncCmd = new ClusterSyncCommand(Integer.parseInt(Config.ClusterDeltaSyncInterval.getDefaultValue()), clusterId); + try { + long seq_no = _agentMgr.send(agentId, new Commands(syncCmd), this); + s_logger.debug("Cluster VM sync started with jobid " + seq_no); + } catch (AgentUnavailableException e) { + s_logger.fatal("The Cluster VM sync process failed for cluster id " + clusterId + " with ", e); + } + } + else { // for others KVM and VMWare + StartupRoutingCommand startup = (StartupRoutingCommand) cmd; + Commands commands = fullHostSync(agentId, startup); + + if (commands.size() > 0) { + s_logger.debug("Sending clean commands to the agent"); + + try { + boolean error = false; + Answer[] answers = _agentMgr.send(agentId, commands); + for (Answer answer : answers) { + if (!answer.getResult()) { + s_logger.warn("Unable to stop a VM due to " + answer.getDetails()); + error = true; + } + } + if (error) { + throw new ConnectionException(true, "Unable to stop VMs"); + } + } catch (final AgentUnavailableException e) { + s_logger.warn("Agent is unavailable now", e); + throw new ConnectionException(true, "Unable to sync", e); + } catch (final OperationTimedoutException e) { + s_logger.warn("Agent is unavailable now", e); + throw new ConnectionException(true, "Unable to sync", e); + } + } + + } + } + + protected class TransitionTask implements Runnable { + @Override + public void run() { + GlobalLock lock = GlobalLock.getInternLock("TransitionChecking"); + if (lock == null) { + s_logger.debug("Couldn't get the global lock"); + return; + } + + if (!lock.lock(30)) { + s_logger.debug("Couldn't lock the db"); + return; + } + try { + lock.addRef(); + List instances = _vmDao.findVMInTransition(new Date(new Date().getTime() - (_operationTimeout * 1000)), State.Starting, State.Stopping); + for (VMInstanceVO instance : instances) { + State state = instance.getState(); + if (state == State.Stopping) { + _haMgr.scheduleStop(instance, instance.getHostId(), WorkType.CheckStop); + } else if (state == State.Starting) { + _haMgr.scheduleRestart(instance, true); + } + } + } catch (Exception e) { + s_logger.warn("Caught the following exception on transition checking", e); + } finally { + StackMaid.current().exitCleanup(); + lock.unlock(); + } + } + } + + protected class AgentVmInfo { + public String name; + public State state; + public String hostUuid; + public VMInstanceVO vm; + public VirtualMachineGuru guru; + + @SuppressWarnings("unchecked") + public AgentVmInfo(String name, VirtualMachineGuru guru, VMInstanceVO vm, State state, String host) { + this.name = name; + this.state = state; + this.vm = vm; + this.guru = (VirtualMachineGuru) guru; + this.hostUuid = host; + } + + public AgentVmInfo(String name, VirtualMachineGuru guru, VMInstanceVO vm, State state) { + this(name, guru, vm, state, null); + } + + public String getHostUuid() { + return hostUuid; + } + } + + @Override + 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 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.", 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() + ")"); + } + + throw new InvalidParameterValueException("Not upgrading vm " + vmInstance.toString() + " since it already " + + "has the requested service offering (" + newServiceOffering.getName() + ")", null); + } + + ServiceOfferingVO currentServiceOffering = _offeringDao.findByIdIncludingRemoved(vmInstance.getServiceOfferingId()); + + // Check that the service offering being upgraded to has the same Guest IP type as the VM's current service offering + // NOTE: With the new network refactoring in 2.2, we shouldn't need the check for same guest IP type anymore. + /* + * if (!currentServiceOffering.getGuestIpType().equals(newServiceOffering.getGuestIpType())) { String errorMsg = + * "The service offering being upgraded to has a guest IP type: " + newServiceOffering.getGuestIpType(); errorMsg += + * ". Please select a service offering with the same guest IP type as the VM's current service offering (" + + * currentServiceOffering.getGuestIpType() + ")."; throw new InvalidParameterValueException(errorMsg); } + */ + + // Check that the service offering being upgraded to has the same storage pool preference as the VM's current service + // offering + if (currentServiceOffering.getUseLocalStorage() != newServiceOffering.getUseLocalStorage()) { + 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(), 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", 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", null); + } + + // Check that the service offering being upgraded to has all the tags of the current service offering + List currentTags = _configMgr.csvTagsToList(currentServiceOffering.getTags()); + 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 " + + "current service offering. Current service offering tags: " + currentTags + "; " + "new service " + + "offering tags: " + newTags, null); + } + } + + @Override + public boolean upgradeVmDb(long vmId, long serviceOfferingId) { + VMInstanceVO vmForUpdate = _vmDao.createForUpdate(); + vmForUpdate.setServiceOfferingId(serviceOfferingId); + ServiceOffering newSvcOff = _configMgr.getServiceOffering(serviceOfferingId); + vmForUpdate.setHaEnabled(newSvcOff.getOfferHA()); + vmForUpdate.setLimitCpuUse(newSvcOff.getLimitCpuUse()); + vmForUpdate.setServiceOfferingId(newSvcOff.getId()); + return _vmDao.update(vmId, vmForUpdate); + } + + @Override + public NicProfile addVmToNetwork(VirtualMachine vm, Network network, NicProfile requested) throws ConcurrentOperationException, + 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"); + return nic; + } else { + s_logger.warn("Failed to plug nic to the vm " + vm + " in network " + network); + return null; + } + } else if (vm.getState() == State.Stopped) { + //1) allocate nic + return _networkMgr.createNicForVm(network, requested, context, vmProfile, false); + } else { + s_logger.warn("Unable to add vm " + vm + " to network " + network); + throw new ResourceUnavailableException("Unable to add vm " + vm + " to network, is not in the right state", + DataCenter.class, vm.getDataCenterIdToDeployIn()); + } + } + + + @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); + boolean result = vmGuru.unplugNic(network, nicTO, vmTO, context, dest); + if (result) { + s_logger.debug("Nic is unplugged successfully for vm " + vm + " in network " + network ); + } else { + 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; + } + +} diff --git a/server/src/com/cloud/vm/dao/DomainRouterDaoImpl.java b/server/src/com/cloud/vm/dao/DomainRouterDaoImpl.java index b79f2258baf..d04aa210381 100755 --- a/server/src/com/cloud/vm/dao/DomainRouterDaoImpl.java +++ b/server/src/com/cloud/vm/dao/DomainRouterDaoImpl.java @@ -288,12 +288,14 @@ public class DomainRouterDaoImpl extends GenericDaoBase im RouterNetworkVO routerNtwkMap = new RouterNetworkVO(router.getId(), guestNetwork.getId(), guestNetwork.getGuestType()); _routerNetworkDao.persist(routerNtwkMap); //2) create user stats entry for the network - UserStatisticsVO stats = _userStatsDao.findBy(router.getAccountId(), router.getDataCenterIdToDeployIn(), - guestNetwork.getId(), null, router.getId(), router.getType().toString()); - if (stats == null) { - stats = new UserStatisticsVO(router.getAccountId(), router.getDataCenterIdToDeployIn(), null, router.getId(), - router.getType().toString(), guestNetwork.getId()); - _userStatsDao.persist(stats); + if(router.getVpcId() == null){ + UserStatisticsVO stats = _userStatsDao.findBy(router.getAccountId(), router.getDataCenterIdToDeployIn(), + guestNetwork.getId(), null, router.getId(), router.getType().toString()); + if (stats == null) { + stats = new UserStatisticsVO(router.getAccountId(), router.getDataCenterIdToDeployIn(), null, router.getId(), + router.getType().toString(), guestNetwork.getId()); + _userStatsDao.persist(stats); + } } txn.commit(); } diff --git a/server/src/com/cloud/vm/dao/NicDao.java b/server/src/com/cloud/vm/dao/NicDao.java index 6b8ea3069ac..5c04b7ca02f 100644 --- a/server/src/com/cloud/vm/dao/NicDao.java +++ b/server/src/com/cloud/vm/dao/NicDao.java @@ -33,6 +33,7 @@ public interface NicDao extends GenericDao { void removeNicsForInstance(long instanceId); NicVO findByNetworkIdAndType(long networkId, VirtualMachine.Type vmType); + NicVO findByNetworkIdTypeAndGateway(long networkId, VirtualMachine.Type vmType, String gateway); NicVO findByIp4AddressAndNetworkId(String ip4Address, long networkId); diff --git a/server/src/com/cloud/vm/dao/NicDaoImpl.java b/server/src/com/cloud/vm/dao/NicDaoImpl.java index dd568e555f2..3a1b7094db7 100644 --- a/server/src/com/cloud/vm/dao/NicDaoImpl.java +++ b/server/src/com/cloud/vm/dao/NicDaoImpl.java @@ -43,6 +43,7 @@ public class NicDaoImpl extends GenericDaoBase implements NicDao { AllFieldsSearch.and("network", AllFieldsSearch.entity().getNetworkId(), Op.EQ); AllFieldsSearch.and("vmType", AllFieldsSearch.entity().getVmType(), Op.EQ); AllFieldsSearch.and("address", AllFieldsSearch.entity().getIp4Address(), Op.EQ); + AllFieldsSearch.and("gateway", AllFieldsSearch.entity().getGateway(), Op.EQ); AllFieldsSearch.and("isDefault", AllFieldsSearch.entity().isDefaultNic(), Op.EQ); AllFieldsSearch.and("broadcastUri", AllFieldsSearch.entity().getBroadcastUri(), Op.EQ); AllFieldsSearch.done(); @@ -126,6 +127,15 @@ public class NicDaoImpl extends GenericDaoBase implements NicDao { return findOneBy(sc); } + @Override + public NicVO findByNetworkIdTypeAndGateway(long networkId, VirtualMachine.Type vmType, String gateway) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("network", networkId); + sc.setParameters("vmType", vmType); + sc.setParameters("gateway", gateway); + return findOneBy(sc); + } + @Override public NicVO findByIp4AddressAndNetworkId(String ip4Address, long networkId) { SearchCriteria sc = AllFieldsSearch.create(); diff --git a/server/test/com/cloud/network/MockNetworkManagerImpl.java b/server/test/com/cloud/network/MockNetworkManagerImpl.java index 9590f825239..80fa5da8734 100755 --- a/server/test/com/cloud/network/MockNetworkManagerImpl.java +++ b/server/test/com/cloud/network/MockNetworkManagerImpl.java @@ -51,7 +51,6 @@ import com.cloud.network.element.UserDataServiceProvider; import com.cloud.network.guru.NetworkGuru; import com.cloud.network.rules.FirewallRule; import com.cloud.network.rules.StaticNat; -import com.cloud.network.vpc.Vpc; import com.cloud.offering.NetworkOffering; import com.cloud.offerings.NetworkOfferingVO; import com.cloud.user.Account; @@ -946,15 +945,6 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS return null; } - /* (non-Javadoc) - * @see com.cloud.network.NetworkManager#unassignIPFromVpcNetwork(long) - */ - @Override - public void unassignIPFromVpcNetwork(long ipId, long networkId) { - // TODO Auto-generated method stub - - } - /* (non-Javadoc) * @see com.cloud.network.NetworkManager#getNicProfile(com.cloud.vm.VirtualMachine, long) */ @@ -973,16 +963,6 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS return false; } - - /* (non-Javadoc) - * @see com.cloud.network.NetworkService#allocateIP(com.cloud.user.Account, boolean, long) - */ - @Override - public IpAddress allocateIP(Account ipOwner, boolean isSystem, long zoneId) throws ResourceAllocationException, InsufficientAddressCapacityException, ConcurrentOperationException { - // TODO Auto-generated method stub - return null; - } - /* (non-Javadoc) * @see com.cloud.network.NetworkService#listNetworksByVpc(long) */ @@ -1010,15 +990,6 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS return null; } - /* (non-Javadoc) - * @see com.cloud.network.NetworkManager#assignSourceNatIpAddressToVpc(com.cloud.user.Account, com.cloud.network.vpc.Vpc) - */ - @Override - public PublicIp assignSourceNatIpAddressToVpc(Account owner, Vpc vpc) throws InsufficientAddressCapacityException, ConcurrentOperationException { - // TODO Auto-generated method stub - return null; - } - /* (non-Javadoc) * @see com.cloud.network.NetworkService#canUseForDeploy(com.cloud.network.Network) */ @@ -1085,11 +1056,29 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS } /* (non-Javadoc) - * @see com.cloud.network.NetworkManager#ipUsedInVpc(com.cloud.network.IpAddress) + * @see com.cloud.network.NetworkService#allocateIP(com.cloud.user.Account, long) */ @Override - public boolean ipUsedInVpc(IpAddress ip) { + public IpAddress allocateIP(Account ipOwner, long zoneId) throws ResourceAllocationException, InsufficientAddressCapacityException, ConcurrentOperationException { // TODO Auto-generated method stub - return false; + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#markPublicIpAsAllocated(com.cloud.network.IPAddressVO) + */ + @Override + public void markPublicIpAsAllocated(IPAddressVO addr) { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#assignDedicateIpAddress(com.cloud.user.Account, java.lang.Long, java.lang.Long, long, boolean) + */ + @Override + public PublicIp assignDedicateIpAddress(Account owner, Long guestNtwkId, Long vpcId, long dcId, boolean isSourceNat) throws ConcurrentOperationException, InsufficientAddressCapacityException { + // TODO Auto-generated method stub + return null; } } diff --git a/server/test/com/cloud/user/MockAccountManagerImpl.java b/server/test/com/cloud/user/MockAccountManagerImpl.java index e7ee1edd1ee..9118607d6b7 100644 --- a/server/test/com/cloud/user/MockAccountManagerImpl.java +++ b/server/test/com/cloud/user/MockAccountManagerImpl.java @@ -39,7 +39,7 @@ import com.cloud.utils.db.SearchCriteria; @Local(value = { AccountManager.class, AccountService.class }) -public class MockAccountManagerImpl implements Manager, AccountManager { +public class MockAccountManagerImpl implements Manager, AccountManager, AccountService { @Override @@ -98,14 +98,12 @@ public class MockAccountManagerImpl implements Manager, AccountManager { @Override public Account getSystemAccount() { - // TODO Auto-generated method stub - return null; + return new AccountVO(); } @Override public User getSystemUser() { - // TODO Auto-generated method stub - return null; + return new UserVO(); } @Override diff --git a/server/test/com/cloud/vpc/MockConfigurationManagerImpl.java b/server/test/com/cloud/vpc/MockConfigurationManagerImpl.java new file mode 100644 index 00000000000..855942aa598 --- /dev/null +++ b/server/test/com/cloud/vpc/MockConfigurationManagerImpl.java @@ -0,0 +1,599 @@ +// 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.vpc; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.ejb.Local; +import javax.naming.ConfigurationException; +import javax.naming.NamingException; + +import com.cloud.api.commands.CreateDiskOfferingCmd; +import com.cloud.api.commands.CreateNetworkOfferingCmd; +import com.cloud.api.commands.CreateServiceOfferingCmd; +import com.cloud.api.commands.CreateVlanIpRangeCmd; +import com.cloud.api.commands.CreateZoneCmd; +import com.cloud.api.commands.DeleteDiskOfferingCmd; +import com.cloud.api.commands.DeleteNetworkOfferingCmd; +import com.cloud.api.commands.DeletePodCmd; +import com.cloud.api.commands.DeleteServiceOfferingCmd; +import com.cloud.api.commands.DeleteVlanIpRangeCmd; +import com.cloud.api.commands.DeleteZoneCmd; +import com.cloud.api.commands.LDAPConfigCmd; +import com.cloud.api.commands.LDAPRemoveCmd; +import com.cloud.api.commands.ListNetworkOfferingsCmd; +import com.cloud.api.commands.UpdateCfgCmd; +import com.cloud.api.commands.UpdateDiskOfferingCmd; +import com.cloud.api.commands.UpdateNetworkOfferingCmd; +import com.cloud.api.commands.UpdatePodCmd; +import com.cloud.api.commands.UpdateServiceOfferingCmd; +import com.cloud.api.commands.UpdateZoneCmd; +import com.cloud.configuration.Configuration; +import com.cloud.configuration.ConfigurationManager; +import com.cloud.configuration.ConfigurationService; +import com.cloud.dc.ClusterVO; +import com.cloud.dc.DataCenter; +import com.cloud.dc.DataCenter.NetworkType; +import com.cloud.dc.DataCenterVO; +import com.cloud.dc.HostPodVO; +import com.cloud.dc.Pod; +import com.cloud.dc.Vlan; +import com.cloud.exception.ConcurrentOperationException; +import com.cloud.exception.InsufficientCapacityException; +import com.cloud.exception.InvalidParameterValueException; +import com.cloud.exception.ResourceAllocationException; +import com.cloud.exception.ResourceUnavailableException; +import com.cloud.network.Network.Capability; +import com.cloud.network.Network.GuestType; +import com.cloud.network.Network.Provider; +import com.cloud.network.Network.Service; +import com.cloud.network.Networks.TrafficType; +import com.cloud.offering.DiskOffering; +import com.cloud.offering.NetworkOffering; +import com.cloud.offering.NetworkOffering.Availability; +import com.cloud.offering.ServiceOffering; +import com.cloud.offerings.NetworkOfferingVO; +import com.cloud.offerings.dao.NetworkOfferingDao; +import com.cloud.org.Grouping.AllocationState; +import com.cloud.service.ServiceOfferingVO; +import com.cloud.storage.DiskOfferingVO; +import com.cloud.user.Account; +import com.cloud.utils.component.Inject; +import com.cloud.utils.component.Manager; +import com.cloud.vm.VirtualMachine.Type; + +@Local(value = { ConfigurationManager.class, ConfigurationService.class }) +public class MockConfigurationManagerImpl implements ConfigurationManager, ConfigurationService, Manager{ + @Inject + NetworkOfferingDao _ntwkOffDao; + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationService#updateConfiguration(com.cloud.api.commands.UpdateCfgCmd) + */ + @Override + public Configuration updateConfiguration(UpdateCfgCmd cmd) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationService#createServiceOffering(com.cloud.api.commands.CreateServiceOfferingCmd) + */ + @Override + public ServiceOffering createServiceOffering(CreateServiceOfferingCmd cmd) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationService#updateServiceOffering(com.cloud.api.commands.UpdateServiceOfferingCmd) + */ + @Override + public ServiceOffering updateServiceOffering(UpdateServiceOfferingCmd cmd) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationService#deleteServiceOffering(com.cloud.api.commands.DeleteServiceOfferingCmd) + */ + @Override + public boolean deleteServiceOffering(DeleteServiceOfferingCmd cmd) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationService#updateDiskOffering(com.cloud.api.commands.UpdateDiskOfferingCmd) + */ + @Override + public DiskOffering updateDiskOffering(UpdateDiskOfferingCmd cmd) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationService#deleteDiskOffering(com.cloud.api.commands.DeleteDiskOfferingCmd) + */ + @Override + public boolean deleteDiskOffering(DeleteDiskOfferingCmd cmd) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationService#createDiskOffering(com.cloud.api.commands.CreateDiskOfferingCmd) + */ + @Override + public DiskOffering createDiskOffering(CreateDiskOfferingCmd cmd) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationService#createPod(long, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) + */ + @Override + public Pod createPod(long zoneId, String name, String startIp, String endIp, String gateway, String netmask, String allocationState) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationService#editPod(com.cloud.api.commands.UpdatePodCmd) + */ + @Override + public Pod editPod(UpdatePodCmd cmd) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationService#deletePod(com.cloud.api.commands.DeletePodCmd) + */ + @Override + public boolean deletePod(DeletePodCmd cmd) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationService#createZone(com.cloud.api.commands.CreateZoneCmd) + */ + @Override + public DataCenter createZone(CreateZoneCmd cmd) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationService#editZone(com.cloud.api.commands.UpdateZoneCmd) + */ + @Override + public DataCenter editZone(UpdateZoneCmd cmd) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationService#deleteZone(com.cloud.api.commands.DeleteZoneCmd) + */ + @Override + public boolean deleteZone(DeleteZoneCmd cmd) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationService#createVlanAndPublicIpRange(com.cloud.api.commands.CreateVlanIpRangeCmd) + */ + @Override + public Vlan createVlanAndPublicIpRange(CreateVlanIpRangeCmd cmd) throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException, ResourceAllocationException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationService#markDefaultZone(java.lang.String, long, long) + */ + @Override + public Account markDefaultZone(String accountName, long domainId, long defaultZoneId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationService#deleteVlanIpRange(com.cloud.api.commands.DeleteVlanIpRangeCmd) + */ + @Override + public boolean deleteVlanIpRange(DeleteVlanIpRangeCmd cmd) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationService#createNetworkOffering(com.cloud.api.commands.CreateNetworkOfferingCmd) + */ + @Override + public NetworkOffering createNetworkOffering(CreateNetworkOfferingCmd cmd) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationService#updateNetworkOffering(com.cloud.api.commands.UpdateNetworkOfferingCmd) + */ + @Override + public NetworkOffering updateNetworkOffering(UpdateNetworkOfferingCmd cmd) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationService#searchForNetworkOfferings(com.cloud.api.commands.ListNetworkOfferingsCmd) + */ + @Override + public List searchForNetworkOfferings(ListNetworkOfferingsCmd cmd) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationService#deleteNetworkOffering(com.cloud.api.commands.DeleteNetworkOfferingCmd) + */ + @Override + public boolean deleteNetworkOffering(DeleteNetworkOfferingCmd cmd) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationService#getNetworkOffering(long) + */ + @Override + public NetworkOffering getNetworkOffering(long id) { + return _ntwkOffDao.findById(id); + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationService#getNetworkOfferingNetworkRate(long) + */ + @Override + public Integer getNetworkOfferingNetworkRate(long networkOfferingId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationService#getVlanAccount(long) + */ + @Override + public Account getVlanAccount(long vlanId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationService#listNetworkOfferings(com.cloud.network.Networks.TrafficType, boolean) + */ + @Override + public List listNetworkOfferings(TrafficType trafficType, boolean systemOnly) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationService#getZone(long) + */ + @Override + public DataCenter getZone(long id) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationService#getServiceOffering(long) + */ + @Override + public ServiceOffering getServiceOffering(long serviceOfferingId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationService#getDefaultPageSize() + */ + @Override + public Long getDefaultPageSize() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationService#getServiceOfferingNetworkRate(long) + */ + @Override + public Integer getServiceOfferingNetworkRate(long serviceOfferingId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationService#getDiskOffering(long) + */ + @Override + public DiskOffering getDiskOffering(long diskOfferingId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationService#updateLDAP(com.cloud.api.commands.LDAPConfigCmd) + */ + @Override + public boolean updateLDAP(LDAPConfigCmd cmd) throws NamingException { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationService#removeLDAP(com.cloud.api.commands.LDAPRemoveCmd) + */ + @Override + public boolean removeLDAP(LDAPRemoveCmd cmd) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationService#isOfferingForVpc(com.cloud.offering.NetworkOffering) + */ + @Override + public boolean isOfferingForVpc(NetworkOffering offering) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.utils.component.Manager#configure(java.lang.String, java.util.Map) + */ + @Override + public boolean configure(String name, Map params) throws ConfigurationException { + // TODO Auto-generated method stub + return true; + } + + /* (non-Javadoc) + * @see com.cloud.utils.component.Manager#start() + */ + @Override + public boolean start() { + // TODO Auto-generated method stub + return true; + } + + /* (non-Javadoc) + * @see com.cloud.utils.component.Manager#stop() + */ + @Override + public boolean stop() { + // TODO Auto-generated method stub + return true; + } + + /* (non-Javadoc) + * @see com.cloud.utils.component.Manager#getName() + */ + @Override + public String getName() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationManager#updateConfiguration(long, java.lang.String, java.lang.String, java.lang.String) + */ + @Override + public void updateConfiguration(long userId, String name, String category, String value) { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationManager#createServiceOffering(long, boolean, com.cloud.vm.VirtualMachine.Type, java.lang.String, int, int, int, java.lang.String, boolean, boolean, boolean, java.lang.String, java.lang.Long, java.lang.String, java.lang.Integer) + */ + @Override + public ServiceOfferingVO createServiceOffering(long userId, boolean isSystem, Type vm_typeType, String name, int cpu, int ramSize, int speed, String displayText, boolean localStorageRequired, boolean offerHA, + boolean limitResourceUse, String tags, Long domainId, String hostTag, Integer networkRate) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationManager#createDiskOffering(java.lang.Long, java.lang.String, java.lang.String, java.lang.Long, java.lang.String, boolean, boolean) + */ + @Override + public DiskOfferingVO createDiskOffering(Long domainId, String name, String description, Long numGibibytes, String tags, boolean isCustomized, boolean localStorageRequired) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationManager#createPod(long, java.lang.String, long, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, boolean) + */ + @Override + public HostPodVO createPod(long userId, String podName, long zoneId, String gateway, String cidr, String startIp, String endIp, String allocationState, boolean skipGatewayOverlapCheck) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationManager#createZone(long, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.Long, com.cloud.dc.DataCenter.NetworkType, java.lang.String, java.lang.String, boolean, boolean) + */ + @Override + public DataCenterVO createZone(long userId, String zoneName, String dns1, String dns2, String internalDns1, String internalDns2, String guestCidr, String domain, Long domainId, NetworkType zoneType, + String allocationState, String networkDomain, boolean isSecurityGroupEnabled, boolean isLocalStorageEnabled) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationManager#deleteVlanAndPublicIpRange(long, long, com.cloud.user.Account) + */ + @Override + public boolean deleteVlanAndPublicIpRange(long userId, long vlanDbId, Account caller) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationManager#csvTagsToList(java.lang.String) + */ + @Override + public List csvTagsToList(String tags) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationManager#listToCsvTags(java.util.List) + */ + @Override + public String listToCsvTags(List tags) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationManager#checkZoneAccess(com.cloud.user.Account, com.cloud.dc.DataCenter) + */ + @Override + public void checkZoneAccess(Account caller, DataCenter zone) { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationManager#checkDiskOfferingAccess(com.cloud.user.Account, com.cloud.offering.DiskOffering) + */ + @Override + public void checkDiskOfferingAccess(Account caller, DiskOffering dof) { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationManager#createNetworkOffering(java.lang.String, java.lang.String, com.cloud.network.Networks.TrafficType, java.lang.String, boolean, com.cloud.offering.NetworkOffering.Availability, java.lang.Integer, java.util.Map, boolean, com.cloud.network.Network.GuestType, boolean, java.lang.Long, boolean, java.util.Map, boolean) + */ + @Override + public NetworkOfferingVO createNetworkOffering(String name, String displayText, TrafficType trafficType, String tags, boolean specifyVlan, Availability availability, Integer networkRate, + Map> serviceProviderMap, boolean isDefault, GuestType type, boolean systemOnly, Long serviceOfferingId, boolean conserveMode, + Map> serviceCapabilityMap, boolean specifyIpRanges) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationManager#createVlanAndPublicIpRange(long, long, long, boolean, java.lang.Long, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, com.cloud.user.Account) + */ + @Override + public Vlan createVlanAndPublicIpRange(long zoneId, long networkId, long physicalNetworkId, boolean forVirtualNetwork, Long podId, String startIP, String endIP, String vlanGateway, String vlanNetmask, String vlanId, + Account vlanOwner) throws InsufficientCapacityException, ConcurrentOperationException, InvalidParameterValueException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationManager#createDefaultSystemNetworks(long) + */ + @Override + public void createDefaultSystemNetworks(long zoneId) throws ConcurrentOperationException { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationManager#getPod(long) + */ + @Override + public HostPodVO getPod(long id) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationManager#getCluster(long) + */ + @Override + public ClusterVO getCluster(long id) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationManager#deleteAccountSpecificVirtualRanges(long) + */ + @Override + public boolean deleteAccountSpecificVirtualRanges(long accountId) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationManager#editPod(long, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) + */ + @Override + public Pod editPod(long id, String name, String startIp, String endIp, String gateway, String netmask, String allocationStateStr) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationManager#checkPodCidrSubnets(long, java.lang.Long, java.lang.String) + */ + @Override + public void checkPodCidrSubnets(long zoneId, Long podIdToBeSkipped, String cidr) { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationManager#findPodAllocationState(com.cloud.dc.HostPodVO) + */ + @Override + public AllocationState findPodAllocationState(HostPodVO pod) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationManager#findClusterAllocationState(com.cloud.dc.ClusterVO) + */ + @Override + public AllocationState findClusterAllocationState(ClusterVO cluster) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.ConfigurationManager#cleanupTags(java.lang.String) + */ + @Override + public String cleanupTags(String tags) { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/server/test/com/cloud/vpc/MockNetworkManagerImpl.java b/server/test/com/cloud/vpc/MockNetworkManagerImpl.java new file mode 100644 index 00000000000..d33f8b4333c --- /dev/null +++ b/server/test/com/cloud/vpc/MockNetworkManagerImpl.java @@ -0,0 +1,1496 @@ +// 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.vpc; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.ejb.Local; +import javax.naming.ConfigurationException; + +import org.apache.log4j.Logger; + +import com.cloud.acl.ControlledEntity.ACLType; +import com.cloud.api.commands.CreateNetworkCmd; +import com.cloud.api.commands.ListNetworksCmd; +import com.cloud.api.commands.ListTrafficTypeImplementorsCmd; +import com.cloud.api.commands.RestartNetworkCmd; +import com.cloud.dc.DataCenter; +import com.cloud.dc.Vlan; +import com.cloud.dc.Vlan.VlanType; +import com.cloud.deploy.DataCenterDeployment; +import com.cloud.deploy.DeployDestination; +import com.cloud.deploy.DeploymentPlan; +import com.cloud.exception.ConcurrentOperationException; +import com.cloud.exception.InsufficientAddressCapacityException; +import com.cloud.exception.InsufficientCapacityException; +import com.cloud.exception.InsufficientVirtualNetworkCapcityException; +import com.cloud.exception.ResourceAllocationException; +import com.cloud.exception.ResourceUnavailableException; +import com.cloud.hypervisor.Hypervisor.HypervisorType; +import com.cloud.network.IPAddressVO; +import com.cloud.network.IpAddress; +import com.cloud.network.Network; +import com.cloud.network.Network.Capability; +import com.cloud.network.Network.GuestType; +import com.cloud.network.Network.Provider; +import com.cloud.network.Network.Service; +import com.cloud.network.NetworkManager; +import com.cloud.network.NetworkProfile; +import com.cloud.network.NetworkService; +import com.cloud.network.NetworkVO; +import com.cloud.network.Networks.TrafficType; +import com.cloud.network.PhysicalNetwork; +import com.cloud.network.PhysicalNetworkServiceProvider; +import com.cloud.network.PhysicalNetworkSetupInfo; +import com.cloud.network.PhysicalNetworkTrafficType; +import com.cloud.network.PublicIpAddress; +import com.cloud.network.addr.PublicIp; +import com.cloud.network.dao.NetworkServiceMapDao; +import com.cloud.network.element.NetworkElement; +import com.cloud.network.element.RemoteAccessVPNServiceProvider; +import com.cloud.network.element.Site2SiteVpnServiceProvider; +import com.cloud.network.element.UserDataServiceProvider; +import com.cloud.network.guru.NetworkGuru; +import com.cloud.network.rules.FirewallRule; +import com.cloud.network.rules.StaticNat; +import com.cloud.offering.NetworkOffering; +import com.cloud.offerings.NetworkOfferingVO; +import com.cloud.offerings.dao.NetworkOfferingServiceMapDao; +import com.cloud.user.Account; +import com.cloud.user.User; +import com.cloud.utils.Pair; +import com.cloud.utils.component.Adapters; +import com.cloud.utils.component.Inject; +import com.cloud.utils.component.Manager; +import com.cloud.vm.Nic; +import com.cloud.vm.NicProfile; +import com.cloud.vm.ReservationContext; +import com.cloud.vm.VMInstanceVO; +import com.cloud.vm.VirtualMachine; +import com.cloud.vm.VirtualMachineProfile; +import com.cloud.vm.VirtualMachineProfileImpl; +import com.cloud.vpc.dao.MockVpcVirtualRouterElement; + +@Local(value = { NetworkManager.class, NetworkService.class }) +public class MockNetworkManagerImpl implements NetworkManager, Manager{ + @Inject + NetworkServiceMapDao _ntwkSrvcDao; + @Inject + NetworkOfferingServiceMapDao _ntwkOfferingSrvcDao; + @Inject(adapter = NetworkElement.class) + Adapters _networkElements; + + private static HashMap s_providerToNetworkElementMap = new HashMap(); + private static final Logger s_logger = Logger.getLogger(MockNetworkManagerImpl.class); + + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#getIsolatedNetworksOwnedByAccountInZone(long, com.cloud.user.Account) + */ + @Override + public List getIsolatedNetworksOwnedByAccountInZone(long zoneId, Account owner) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#allocateIP(com.cloud.user.Account, long) + */ + @Override + public IpAddress allocateIP(Account ipOwner, long zoneId) throws ResourceAllocationException, InsufficientAddressCapacityException, ConcurrentOperationException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#releaseIpAddress(long) + */ + @Override + public boolean releaseIpAddress(long ipAddressId) throws InsufficientAddressCapacityException { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#createGuestNetwork(com.cloud.api.commands.CreateNetworkCmd) + */ + @Override + public Network createGuestNetwork(CreateNetworkCmd cmd) throws InsufficientCapacityException, ConcurrentOperationException, ResourceAllocationException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#searchForNetworks(com.cloud.api.commands.ListNetworksCmd) + */ + @Override + public List searchForNetworks(ListNetworksCmd cmd) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#deleteNetwork(long) + */ + @Override + public boolean deleteNetwork(long networkId) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#restartNetwork(com.cloud.api.commands.RestartNetworkCmd, boolean) + */ + @Override + public boolean restartNetwork(RestartNetworkCmd cmd, boolean cleanup) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#getActiveNicsInNetwork(long) + */ + @Override + public int getActiveNicsInNetwork(long networkId) { + // TODO Auto-generated method stub + return 0; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#getNetwork(long) + */ + @Override + public Network getNetwork(long networkId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#getIp(long) + */ + @Override + public IpAddress getIp(long id) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#convertNetworkToNetworkProfile(long) + */ + @Override + public NetworkProfile convertNetworkToNetworkProfile(long networkId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#getNetworkCapabilities(long) + */ + @Override + public Map> getNetworkCapabilities(long networkId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#isNetworkAvailableInDomain(long, long) + */ + @Override + public boolean isNetworkAvailableInDomain(long networkId, long domainId) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#getDedicatedNetworkDomain(long) + */ + @Override + public Long getDedicatedNetworkDomain(long networkId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#updateGuestNetwork(long, java.lang.String, java.lang.String, com.cloud.user.Account, com.cloud.user.User, java.lang.String, java.lang.Long, java.lang.Boolean) + */ + @Override + public Network updateGuestNetwork(long networkId, String name, String displayText, Account callerAccount, User callerUser, String domainSuffix, Long networkOfferingId, Boolean changeCidr) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#getNetworkRate(long, java.lang.Long) + */ + @Override + public Integer getNetworkRate(long networkId, Long vmId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#getSystemNetworkByZoneAndTrafficType(long, com.cloud.network.Networks.TrafficType) + */ + @Override + public Network getSystemNetworkByZoneAndTrafficType(long zoneId, TrafficType trafficType) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#getNetworkOfferingServiceProvidersMap(long) + */ + @Override + public Map> getNetworkOfferingServiceProvidersMap(long networkOfferingId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#createPhysicalNetwork(java.lang.Long, java.lang.String, java.lang.String, java.util.List, java.lang.String, java.lang.Long, java.util.List, java.lang.String) + */ + @Override + public PhysicalNetwork createPhysicalNetwork(Long zoneId, String vnetRange, String networkSpeed, List isolationMethods, String broadcastDomainRange, Long domainId, List tags, String name) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#searchPhysicalNetworks(java.lang.Long, java.lang.Long, java.lang.String, java.lang.Long, java.lang.Long, java.lang.String) + */ + @Override + public List searchPhysicalNetworks(Long id, Long zoneId, String keyword, Long startIndex, Long pageSize, String name) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#updatePhysicalNetwork(java.lang.Long, java.lang.String, java.util.List, java.lang.String, java.lang.String) + */ + @Override + public PhysicalNetwork updatePhysicalNetwork(Long id, String networkSpeed, List tags, String newVnetRangeString, String state) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#deletePhysicalNetwork(java.lang.Long) + */ + @Override + public boolean deletePhysicalNetwork(Long id) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#listNetworkServices(java.lang.String) + */ + @Override + public List listNetworkServices(String providerName) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#listSupportedNetworkServiceProviders(java.lang.String) + */ + @Override + public List listSupportedNetworkServiceProviders(String serviceName) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#addProviderToPhysicalNetwork(java.lang.Long, java.lang.String, java.lang.Long, java.util.List) + */ + @Override + public PhysicalNetworkServiceProvider addProviderToPhysicalNetwork(Long physicalNetworkId, String providerName, Long destinationPhysicalNetworkId, List enabledServices) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#listNetworkServiceProviders(java.lang.Long, java.lang.String, java.lang.String, java.lang.Long, java.lang.Long) + */ + @Override + public List listNetworkServiceProviders(Long physicalNetworkId, String name, String state, Long startIndex, Long pageSize) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#updateNetworkServiceProvider(java.lang.Long, java.lang.String, java.util.List) + */ + @Override + public PhysicalNetworkServiceProvider updateNetworkServiceProvider(Long id, String state, List enabledServices) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#deleteNetworkServiceProvider(java.lang.Long) + */ + @Override + public boolean deleteNetworkServiceProvider(Long id) throws ConcurrentOperationException, ResourceUnavailableException { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#getPhysicalNetwork(java.lang.Long) + */ + @Override + public PhysicalNetwork getPhysicalNetwork(Long physicalNetworkId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#getCreatedPhysicalNetwork(java.lang.Long) + */ + @Override + public PhysicalNetwork getCreatedPhysicalNetwork(Long physicalNetworkId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#getPhysicalNetworkServiceProvider(java.lang.Long) + */ + @Override + public PhysicalNetworkServiceProvider getPhysicalNetworkServiceProvider(Long providerId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#getCreatedPhysicalNetworkServiceProvider(java.lang.Long) + */ + @Override + public PhysicalNetworkServiceProvider getCreatedPhysicalNetworkServiceProvider(Long providerId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#findPhysicalNetworkId(long, java.lang.String, com.cloud.network.Networks.TrafficType) + */ + @Override + public long findPhysicalNetworkId(long zoneId, String tag, TrafficType trafficType) { + // TODO Auto-generated method stub + return 0; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#addTrafficTypeToPhysicalNetwork(java.lang.Long, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) + */ + @Override + public PhysicalNetworkTrafficType addTrafficTypeToPhysicalNetwork(Long physicalNetworkId, String trafficType, String xenLabel, String kvmLabel, String vmwareLabel, String simulatorLabel, String vlan) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#getPhysicalNetworkTrafficType(java.lang.Long) + */ + @Override + public PhysicalNetworkTrafficType getPhysicalNetworkTrafficType(Long id) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#updatePhysicalNetworkTrafficType(java.lang.Long, java.lang.String, java.lang.String, java.lang.String) + */ + @Override + public PhysicalNetworkTrafficType updatePhysicalNetworkTrafficType(Long id, String xenLabel, String kvmLabel, String vmwareLabel) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#deletePhysicalNetworkTrafficType(java.lang.Long) + */ + @Override + public boolean deletePhysicalNetworkTrafficType(Long id) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#listTrafficTypes(java.lang.Long) + */ + @Override + public List listTrafficTypes(Long physicalNetworkId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#getDefaultPhysicalNetworkByZoneAndTrafficType(long, com.cloud.network.Networks.TrafficType) + */ + @Override + public PhysicalNetwork getDefaultPhysicalNetworkByZoneAndTrafficType(long zoneId, TrafficType trafficType) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#getExclusiveGuestNetwork(long) + */ + @Override + public Network getExclusiveGuestNetwork(long zoneId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#listTrafficTypeImplementor(com.cloud.api.commands.ListTrafficTypeImplementorsCmd) + */ + @Override + public List> listTrafficTypeImplementor(ListTrafficTypeImplementorsCmd cmd) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#getIsolatedNetworksWithSourceNATOwnedByAccountInZone(long, com.cloud.user.Account) + */ + @Override + public List getIsolatedNetworksWithSourceNATOwnedByAccountInZone(long zoneId, Account owner) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#listNetworksByVpc(long) + */ + @Override + public List listNetworksByVpc(long vpcId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#isVmPartOfNetwork(long, long) + */ + @Override + public boolean isVmPartOfNetwork(long vmId, long ntwkId) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#associateIPToNetwork(long, long) + */ + @Override + public IpAddress associateIPToNetwork(long ipId, long networkId) throws InsufficientAddressCapacityException, ResourceAllocationException, ResourceUnavailableException, ConcurrentOperationException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#createPrivateNetwork(java.lang.String, java.lang.String, long, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, long, java.lang.Long) + */ + @Override + public Network createPrivateNetwork(String networkName, String displayText, long physicalNetworkId, String vlan, String startIp, String endIP, String gateway, String netmask, long networkOwnerId, Long vpcId) + throws ResourceAllocationException, ConcurrentOperationException, InsufficientCapacityException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkService#canUseForDeploy(com.cloud.network.Network) + */ + @Override + public boolean canUseForDeploy(Network network) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#assignPublicIpAddress(long, java.lang.Long, com.cloud.user.Account, com.cloud.dc.Vlan.VlanType, java.lang.Long, java.lang.String, boolean) + */ + @Override + public PublicIp assignPublicIpAddress(long dcId, Long podId, Account owner, VlanType type, Long networkId, String requestedIp, boolean isSystem) throws InsufficientAddressCapacityException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#disassociatePublicIpAddress(long, long, com.cloud.user.Account) + */ + @Override + public boolean disassociatePublicIpAddress(long id, long userId, Account caller) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#listPublicIpsAssignedToGuestNtwk(long, long, java.lang.Boolean) + */ + @Override + public List listPublicIpsAssignedToGuestNtwk(long accountId, long associatedNetworkId, Boolean sourceNat) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#setupNetwork(com.cloud.user.Account, com.cloud.offerings.NetworkOfferingVO, com.cloud.deploy.DeploymentPlan, java.lang.String, java.lang.String, boolean) + */ + @Override + public List setupNetwork(Account owner, NetworkOfferingVO offering, DeploymentPlan plan, String name, String displayText, boolean isDefault) throws ConcurrentOperationException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#setupNetwork(com.cloud.user.Account, com.cloud.offerings.NetworkOfferingVO, com.cloud.network.Network, com.cloud.deploy.DeploymentPlan, java.lang.String, java.lang.String, boolean, java.lang.Long, com.cloud.acl.ControlledEntity.ACLType, java.lang.Boolean, java.lang.Long) + */ + @Override + public List setupNetwork(Account owner, NetworkOfferingVO offering, Network predefined, DeploymentPlan plan, String name, String displayText, boolean errorIfAlreadySetup, Long domainId, ACLType aclType, + Boolean subdomainAccess, Long vpcId) throws ConcurrentOperationException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getSystemAccountNetworkOfferings(java.lang.String[]) + */ + @Override + public List getSystemAccountNetworkOfferings(String... offeringNames) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#allocate(com.cloud.vm.VirtualMachineProfile, java.util.List) + */ + @Override + public void allocate(VirtualMachineProfile vm, List> networks) throws InsufficientCapacityException, ConcurrentOperationException { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#prepare(com.cloud.vm.VirtualMachineProfile, com.cloud.deploy.DeployDestination, com.cloud.vm.ReservationContext) + */ + @Override + public void prepare(VirtualMachineProfile profile, DeployDestination dest, ReservationContext context) throws InsufficientCapacityException, ConcurrentOperationException, + ResourceUnavailableException { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#release(com.cloud.vm.VirtualMachineProfile, boolean) + */ + @Override + public void release(VirtualMachineProfile vmProfile, boolean forced) throws ConcurrentOperationException, ResourceUnavailableException { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#cleanupNics(com.cloud.vm.VirtualMachineProfile) + */ + @Override + public void cleanupNics(VirtualMachineProfile vm) { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#expungeNics(com.cloud.vm.VirtualMachineProfile) + */ + @Override + public void expungeNics(VirtualMachineProfile vm) { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getNics(long) + */ + @Override + public List getNics(long vmId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getNicProfiles(com.cloud.vm.VirtualMachine) + */ + @Override + public List getNicProfiles(VirtualMachine vm) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getNextAvailableMacAddressInNetwork(long) + */ + @Override + public String getNextAvailableMacAddressInNetwork(long networkConfigurationId) throws InsufficientAddressCapacityException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#applyRules(java.util.List, boolean) + */ + @Override + public boolean applyRules(List rules, boolean continueOnError) throws ResourceUnavailableException { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#validateRule(com.cloud.network.rules.FirewallRule) + */ + @Override + public boolean validateRule(FirewallRule rule) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getRemoteAccessVpnElements() + */ + @Override + public List getRemoteAccessVpnElements() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getSite2SiteVpnElements() + */ + @Override + public List getSite2SiteVpnElements() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getPublicIpAddress(long) + */ + @Override + public PublicIpAddress getPublicIpAddress(long ipAddressId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#listPodVlans(long) + */ + @Override + public List listPodVlans(long podId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#implementNetwork(long, com.cloud.deploy.DeployDestination, com.cloud.vm.ReservationContext) + */ + @Override + public Pair implementNetwork(long networkId, DeployDestination dest, ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException, + InsufficientCapacityException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#listNetworksUsedByVm(long, boolean) + */ + @Override + public List listNetworksUsedByVm(long vmId, boolean isSystem) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#prepareNicForMigration(com.cloud.vm.VirtualMachineProfile, com.cloud.deploy.DeployDestination) + */ + @Override + public void prepareNicForMigration(VirtualMachineProfile vm, DeployDestination dest) { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#shutdownNetwork(long, com.cloud.vm.ReservationContext, boolean) + */ + @Override + public boolean shutdownNetwork(long networkId, ReservationContext context, boolean cleanupElements) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#destroyNetwork(long, com.cloud.vm.ReservationContext) + */ + @Override + public boolean destroyNetwork(long networkId, ReservationContext context) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#createGuestNetwork(long, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, com.cloud.user.Account, java.lang.Long, com.cloud.network.PhysicalNetwork, long, com.cloud.acl.ControlledEntity.ACLType, java.lang.Boolean, java.lang.Long) + */ + @Override + public Network createGuestNetwork(long networkOfferingId, String name, String displayText, String gateway, String cidr, String vlanId, String networkDomain, Account owner, Long domainId, + PhysicalNetwork physicalNetwork, long zoneId, ACLType aclType, Boolean subdomainAccess, Long vpcId) throws ConcurrentOperationException, InsufficientCapacityException, ResourceAllocationException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#associateIpAddressListToAccount(long, long, long, java.lang.Long, com.cloud.network.Network) + */ + @Override + public boolean associateIpAddressListToAccount(long userId, long accountId, long zoneId, Long vlanId, Network guestNetwork) throws InsufficientCapacityException, ConcurrentOperationException, + ResourceUnavailableException, ResourceAllocationException { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getNicInNetwork(long, long) + */ + @Override + public Nic getNicInNetwork(long vmId, long networkId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getNicsForTraffic(long, com.cloud.network.Networks.TrafficType) + */ + @Override + public List getNicsForTraffic(long vmId, TrafficType type) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getDefaultNetworkForVm(long) + */ + @Override + public Network getDefaultNetworkForVm(long vmId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getDefaultNic(long) + */ + @Override + public Nic getDefaultNic(long vmId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getPasswordResetElements() + */ + @Override + public List getPasswordResetElements() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#networkIsConfiguredForExternalNetworking(long, long) + */ + @Override + public boolean networkIsConfiguredForExternalNetworking(long zoneId, long networkId) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getNetworkServiceCapabilities(long, com.cloud.network.Network.Service) + */ + @Override + public Map getNetworkServiceCapabilities(long networkId, Service service) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#applyIpAssociations(com.cloud.network.Network, boolean) + */ + @Override + public boolean applyIpAssociations(Network network, boolean continueOnError) throws ResourceUnavailableException { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#areServicesSupportedByNetworkOffering(long, com.cloud.network.Network.Service[]) + */ + @Override + public boolean areServicesSupportedByNetworkOffering(long networkOfferingId, Service... services) { + return (_ntwkOfferingSrvcDao.areServicesSupportedByNetworkOffering(networkOfferingId, services)); + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getNetworkWithSecurityGroupEnabled(java.lang.Long) + */ + @Override + public NetworkVO getNetworkWithSecurityGroupEnabled(Long zoneId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#startNetwork(long, com.cloud.deploy.DeployDestination, com.cloud.vm.ReservationContext) + */ + @Override + public boolean startNetwork(long networkId, DeployDestination dest, ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getIpOfNetworkElementInVirtualNetwork(long, long) + */ + @Override + public String getIpOfNetworkElementInVirtualNetwork(long accountId, long dataCenterId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#listNetworksForAccount(long, long, com.cloud.network.Network.GuestType) + */ + @Override + public List listNetworksForAccount(long accountId, long zoneId, GuestType type) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#listAllNetworksInAllZonesByType(com.cloud.network.Network.GuestType) + */ + @Override + public List listAllNetworksInAllZonesByType(GuestType type) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#markIpAsUnavailable(long) + */ + @Override + public IPAddressVO markIpAsUnavailable(long addrId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#acquireGuestIpAddress(com.cloud.network.Network, java.lang.String) + */ + @Override + public String acquireGuestIpAddress(Network network, String requestedIp) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getGlobalGuestDomainSuffix() + */ + @Override + public String getGlobalGuestDomainSuffix() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getStartIpAddress(long) + */ + @Override + public String getStartIpAddress(long networkId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#applyStaticNats(java.util.List, boolean) + */ + @Override + public boolean applyStaticNats(List staticNats, boolean continueOnError) throws ResourceUnavailableException { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getIpInNetwork(long, long) + */ + @Override + public String getIpInNetwork(long vmId, long networkId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getIpInNetworkIncludingRemoved(long, long) + */ + @Override + public String getIpInNetworkIncludingRemoved(long vmId, long networkId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getPodIdForVlan(long) + */ + @Override + public Long getPodIdForVlan(long vlanDbId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#listNetworkOfferingsForUpgrade(long) + */ + @Override + public List listNetworkOfferingsForUpgrade(long networkId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#isSecurityGroupSupportedInNetwork(com.cloud.network.Network) + */ + @Override + public boolean isSecurityGroupSupportedInNetwork(Network network) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#isProviderSupportServiceInNetwork(long, com.cloud.network.Network.Service, com.cloud.network.Network.Provider) + */ + @Override + public boolean isProviderSupportServiceInNetwork(long networkId, Service service, Provider provider) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#isProviderEnabledInPhysicalNetwork(long, java.lang.String) + */ + @Override + public boolean isProviderEnabledInPhysicalNetwork(long physicalNetowrkId, String providerName) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getNetworkTag(com.cloud.hypervisor.Hypervisor.HypervisorType, com.cloud.network.Network) + */ + @Override + public String getNetworkTag(HypervisorType hType, Network network) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getElementServices(com.cloud.network.Network.Provider) + */ + @Override + public List getElementServices(Provider provider) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#canElementEnableIndividualServices(com.cloud.network.Network.Provider) + */ + @Override + public boolean canElementEnableIndividualServices(Provider provider) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#areServicesSupportedInNetwork(long, com.cloud.network.Network.Service[]) + */ + @Override + public boolean areServicesSupportedInNetwork(long networkId, Service... services) { + return (_ntwkSrvcDao.areServicesSupportedInNetwork(networkId, services)); + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#isNetworkSystem(com.cloud.network.Network) + */ + @Override + public boolean isNetworkSystem(Network network) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#reallocate(com.cloud.vm.VirtualMachineProfile, com.cloud.deploy.DataCenterDeployment) + */ + @Override + public boolean reallocate(VirtualMachineProfile vm, DataCenterDeployment dest) throws InsufficientCapacityException, ConcurrentOperationException { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getNetworkOfferingServiceCapabilities(com.cloud.offering.NetworkOffering, com.cloud.network.Network.Service) + */ + @Override + public Map getNetworkOfferingServiceCapabilities(NetworkOffering offering, Service service) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getPhysicalNetworkId(com.cloud.network.Network) + */ + @Override + public Long getPhysicalNetworkId(Network network) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getAllowSubdomainAccessGlobal() + */ + @Override + public boolean getAllowSubdomainAccessGlobal() { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#isProviderForNetwork(com.cloud.network.Network.Provider, long) + */ + @Override + public boolean isProviderForNetwork(Provider provider, long networkId) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#isProviderForNetworkOffering(com.cloud.network.Network.Provider, long) + */ + @Override + public boolean isProviderForNetworkOffering(Provider provider, long networkOfferingId) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#canProviderSupportServices(java.util.Map) + */ + @Override + public void canProviderSupportServices(Map> providersMap) { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getPhysicalNetworkInfo(long, com.cloud.hypervisor.Hypervisor.HypervisorType) + */ + @Override + public List getPhysicalNetworkInfo(long dcId, HypervisorType hypervisorType) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#canAddDefaultSecurityGroup() + */ + @Override + public boolean canAddDefaultSecurityGroup() { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#listNetworkOfferingServices(long) + */ + @Override + public List listNetworkOfferingServices(long networkOfferingId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#areServicesEnabledInZone(long, com.cloud.offering.NetworkOffering, java.util.List) + */ + @Override + public boolean areServicesEnabledInZone(long zoneId, NetworkOffering offering, List services) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getIpToServices(java.util.List, boolean, boolean) + */ + @Override + public Map> getIpToServices(List publicIps, boolean rulesRevoked, boolean includingFirewall) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getProviderToIpList(com.cloud.network.Network, java.util.Map) + */ + @Override + public Map> getProviderToIpList(Network network, Map> ipToServices) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#checkIpForService(com.cloud.network.IPAddressVO, com.cloud.network.Network.Service, java.lang.Long) + */ + @Override + public boolean checkIpForService(IPAddressVO ip, Service service, Long networkId) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#checkVirtualNetworkCidrOverlap(java.lang.Long, java.lang.String) + */ + @Override + public void checkVirtualNetworkCidrOverlap(Long zoneId, String cidr) { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#checkCapabilityForProvider(java.util.Set, com.cloud.network.Network.Service, com.cloud.network.Network.Capability, java.lang.String) + */ + @Override + public void checkCapabilityForProvider(Set providers, Service service, Capability cap, String capValue) { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getDefaultUniqueProviderForService(java.lang.String) + */ + @Override + public Provider getDefaultUniqueProviderForService(String serviceName) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#assignSystemIp(long, com.cloud.user.Account, boolean, boolean) + */ + @Override + public IpAddress assignSystemIp(long networkId, Account owner, boolean forElasticLb, boolean forElasticIp) throws InsufficientAddressCapacityException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#handleSystemIpRelease(com.cloud.network.IpAddress) + */ + @Override + public boolean handleSystemIpRelease(IpAddress ip) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#checkNetworkPermissions(com.cloud.user.Account, com.cloud.network.Network) + */ + @Override + public void checkNetworkPermissions(Account owner, Network network) { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#allocateDirectIp(com.cloud.vm.NicProfile, com.cloud.dc.DataCenter, com.cloud.vm.VirtualMachineProfile, com.cloud.network.Network, java.lang.String) + */ + @Override + public void allocateDirectIp(NicProfile nic, DataCenter dc, VirtualMachineProfile vm, Network network, String requestedIp) throws InsufficientVirtualNetworkCapcityException, + InsufficientAddressCapacityException { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getDefaultManagementTrafficLabel(long, com.cloud.hypervisor.Hypervisor.HypervisorType) + */ + @Override + public String getDefaultManagementTrafficLabel(long zoneId, HypervisorType hypervisorType) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getDefaultStorageTrafficLabel(long, com.cloud.hypervisor.Hypervisor.HypervisorType) + */ + @Override + public String getDefaultStorageTrafficLabel(long zoneId, HypervisorType hypervisorType) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getDefaultPublicTrafficLabel(long, com.cloud.hypervisor.Hypervisor.HypervisorType) + */ + @Override + public String getDefaultPublicTrafficLabel(long dcId, HypervisorType vmware) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getDefaultGuestTrafficLabel(long, com.cloud.hypervisor.Hypervisor.HypervisorType) + */ + @Override + public String getDefaultGuestTrafficLabel(long dcId, HypervisorType vmware) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getElementImplementingProvider(java.lang.String) + */ + @Override + public NetworkElement getElementImplementingProvider(String providerName) { + return new MockVpcVirtualRouterElement(); + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#assignSourceNatIpAddressToGuestNetwork(com.cloud.user.Account, com.cloud.network.Network) + */ + @Override + public PublicIp assignSourceNatIpAddressToGuestNetwork(Account owner, Network guestNetwork) throws InsufficientAddressCapacityException, ConcurrentOperationException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getAccountNetworkDomain(long, long) + */ + @Override + public String getAccountNetworkDomain(long accountId, long zoneId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getDefaultNetworkDomain() + */ + @Override + public String getDefaultNetworkDomain() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getNtwkOffDistinctProviders(long) + */ + @Override + public List getNtwkOffDistinctProviders(long networkId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#allocateNic(com.cloud.vm.NicProfile, com.cloud.network.Network, java.lang.Boolean, int, com.cloud.vm.VirtualMachineProfile) + */ + @Override + public Pair allocateNic(NicProfile requested, Network network, Boolean isDefaultNic, int deviceId, VirtualMachineProfile vm) + throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException, ConcurrentOperationException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#prepareNic(com.cloud.vm.VirtualMachineProfile, com.cloud.deploy.DeployDestination, com.cloud.vm.ReservationContext, long, com.cloud.network.NetworkVO) + */ + @Override + public NicProfile prepareNic(VirtualMachineProfile vmProfile, DeployDestination dest, ReservationContext context, long nicId, NetworkVO network) + throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException, ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#removeNic(com.cloud.vm.VirtualMachineProfile, com.cloud.vm.Nic) + */ + @Override + public void removeNic(VirtualMachineProfile vm, Nic nic) { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#listPublicIpsAssignedToAccount(long, long, java.lang.Boolean) + */ + @Override + public List listPublicIpsAssignedToAccount(long accountId, long dcId, Boolean sourceNat) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#associateIPToGuestNetwork(long, long, boolean) + */ + @Override + public IPAddressVO associateIPToGuestNetwork(long ipAddrId, long networkId, boolean releaseOnFailure) throws ResourceAllocationException, ResourceUnavailableException, InsufficientAddressCapacityException, + ConcurrentOperationException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getNicProfile(com.cloud.vm.VirtualMachine, long, java.lang.String) + */ + @Override + public NicProfile getNicProfile(VirtualMachine vm, long networkId, String broadcastUri) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#setupDns(com.cloud.network.Network, com.cloud.network.Network.Provider) + */ + @Override + public boolean setupDns(Network network, Provider provider) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#releaseNic(com.cloud.vm.VirtualMachineProfile, com.cloud.vm.Nic) + */ + @Override + public void releaseNic(VirtualMachineProfile vmProfile, Nic nic) throws ConcurrentOperationException, ResourceUnavailableException { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#getPhysicalNtwksSupportingTrafficType(long, com.cloud.network.Networks.TrafficType) + */ + @Override + public List getPhysicalNtwksSupportingTrafficType(long zoneId, TrafficType trafficType) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#isPrivateGateway(com.cloud.vm.Nic) + */ + @Override + public boolean isPrivateGateway(Nic guestNic) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#createNicForVm(com.cloud.network.Network, com.cloud.vm.NicProfile, com.cloud.vm.ReservationContext, com.cloud.vm.VirtualMachineProfileImpl, boolean) + */ + @Override + public NicProfile createNicForVm(Network network, NicProfile requested, ReservationContext context, VirtualMachineProfileImpl vmProfile, boolean prepare) + throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException, ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#assignVpnGatewayIpAddress(long, com.cloud.user.Account, long) + */ + @Override + public PublicIp assignVpnGatewayIpAddress(long dcId, Account owner, long vpcId) throws InsufficientAddressCapacityException, ConcurrentOperationException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#markPublicIpAsAllocated(com.cloud.network.IPAddressVO) + */ + @Override + public void markPublicIpAsAllocated(IPAddressVO addr) { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.cloud.network.NetworkManager#assignDedicateIpAddress(com.cloud.user.Account, java.lang.Long, java.lang.Long, long, boolean) + */ + @Override + public PublicIp assignDedicateIpAddress(Account owner, Long guestNtwkId, Long vpcId, long dcId, boolean isSourceNat) throws ConcurrentOperationException, InsufficientAddressCapacityException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.utils.component.Manager#configure(java.lang.String, java.util.Map) + */ + @Override + public boolean configure(String name, Map params) throws ConfigurationException { + // TODO Auto-generated method stub + return true; + } + + /* (non-Javadoc) + * @see com.cloud.utils.component.Manager#start() + */ + @Override + public boolean start() { + for (NetworkElement element : _networkElements) { + Provider implementedProvider = element.getProvider(); + if (implementedProvider != null) { + if (s_providerToNetworkElementMap.containsKey(implementedProvider.getName())) { + s_logger.error("Cannot start MapNetworkManager: Provider <-> NetworkElement must be a one-to-one map, " + + "multiple NetworkElements found for Provider: " + implementedProvider.getName()); + return false; + } + s_providerToNetworkElementMap.put(implementedProvider.getName(), element.getName()); + } + } + return true; + } + + /* (non-Javadoc) + * @see com.cloud.utils.component.Manager#stop() + */ + @Override + public boolean stop() { + // TODO Auto-generated method stub + return true; + } + + /* (non-Javadoc) + * @see com.cloud.utils.component.Manager#getName() + */ + @Override + public String getName() { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/server/test/com/cloud/vpc/MockResourceLimitManagerImpl.java b/server/test/com/cloud/vpc/MockResourceLimitManagerImpl.java new file mode 100644 index 00000000000..0a199f56eb5 --- /dev/null +++ b/server/test/com/cloud/vpc/MockResourceLimitManagerImpl.java @@ -0,0 +1,150 @@ +// 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.vpc; + +import java.util.List; +import java.util.Map; + +import javax.ejb.Local; +import javax.naming.ConfigurationException; + +import com.cloud.configuration.Resource.ResourceType; +import com.cloud.configuration.ResourceCount; +import com.cloud.configuration.ResourceLimit; +import com.cloud.domain.Domain; +import com.cloud.exception.ResourceAllocationException; +import com.cloud.user.Account; +import com.cloud.user.ResourceLimitService; +import com.cloud.utils.component.Manager; + +@Local(value = { ResourceLimitService.class }) +public class MockResourceLimitManagerImpl implements ResourceLimitService, Manager{ + + /* (non-Javadoc) + * @see com.cloud.user.ResourceLimitService#updateResourceLimit(java.lang.Long, java.lang.Long, java.lang.Integer, java.lang.Long) + */ + @Override + public ResourceLimit updateResourceLimit(Long accountId, Long domainId, Integer resourceType, Long max) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.user.ResourceLimitService#recalculateResourceCount(java.lang.Long, java.lang.Long, java.lang.Integer) + */ + @Override + public List recalculateResourceCount(Long accountId, Long domainId, Integer typeId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.user.ResourceLimitService#searchForLimits(java.lang.Long, java.lang.Long, java.lang.Long, java.lang.Integer, java.lang.Long, java.lang.Long) + */ + @Override + public List searchForLimits(Long id, Long accountId, Long domainId, Integer type, Long startIndex, Long pageSizeVal) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.user.ResourceLimitService#findCorrectResourceLimitForAccount(com.cloud.user.Account, com.cloud.configuration.Resource.ResourceType) + */ + @Override + public long findCorrectResourceLimitForAccount(Account account, ResourceType type) { + // TODO Auto-generated method stub + return 0; + } + + /* (non-Javadoc) + * @see com.cloud.user.ResourceLimitService#findCorrectResourceLimitForDomain(com.cloud.domain.Domain, com.cloud.configuration.Resource.ResourceType) + */ + @Override + public long findCorrectResourceLimitForDomain(Domain domain, ResourceType type) { + // TODO Auto-generated method stub + return 0; + } + + /* (non-Javadoc) + * @see com.cloud.user.ResourceLimitService#incrementResourceCount(long, com.cloud.configuration.Resource.ResourceType, java.lang.Long[]) + */ + @Override + public void incrementResourceCount(long accountId, ResourceType type, Long... delta) { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.cloud.user.ResourceLimitService#decrementResourceCount(long, com.cloud.configuration.Resource.ResourceType, java.lang.Long[]) + */ + @Override + public void decrementResourceCount(long accountId, ResourceType type, Long... delta) { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.cloud.user.ResourceLimitService#checkResourceLimit(com.cloud.user.Account, com.cloud.configuration.Resource.ResourceType, long[]) + */ + @Override + public void checkResourceLimit(Account account, ResourceType type, long... count) throws ResourceAllocationException { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.cloud.user.ResourceLimitService#getResourceCount(com.cloud.user.Account, com.cloud.configuration.Resource.ResourceType) + */ + @Override + public long getResourceCount(Account account, ResourceType type) { + // TODO Auto-generated method stub + return 0; + } + + /* (non-Javadoc) + * @see com.cloud.utils.component.Manager#configure(java.lang.String, java.util.Map) + */ + @Override + public boolean configure(String name, Map params) throws ConfigurationException { + // TODO Auto-generated method stub + return true; + } + + /* (non-Javadoc) + * @see com.cloud.utils.component.Manager#start() + */ + @Override + public boolean start() { + // TODO Auto-generated method stub + return true; + } + + /* (non-Javadoc) + * @see com.cloud.utils.component.Manager#stop() + */ + @Override + public boolean stop() { + // TODO Auto-generated method stub + return true; + } + + /* (non-Javadoc) + * @see com.cloud.utils.component.Manager#getName() + */ + @Override + public String getName() { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/server/test/com/cloud/vpc/MockSite2SiteVpnManagerImpl.java b/server/test/com/cloud/vpc/MockSite2SiteVpnManagerImpl.java new file mode 100644 index 00000000000..cb200f4ef1c --- /dev/null +++ b/server/test/com/cloud/vpc/MockSite2SiteVpnManagerImpl.java @@ -0,0 +1,253 @@ +// 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.vpc; + +import java.util.List; +import java.util.Map; + +import javax.ejb.Local; +import javax.naming.ConfigurationException; + +import com.cloud.api.commands.CreateVpnConnectionCmd; +import com.cloud.api.commands.CreateVpnCustomerGatewayCmd; +import com.cloud.api.commands.CreateVpnGatewayCmd; +import com.cloud.api.commands.DeleteVpnConnectionCmd; +import com.cloud.api.commands.DeleteVpnCustomerGatewayCmd; +import com.cloud.api.commands.DeleteVpnGatewayCmd; +import com.cloud.api.commands.ListVpnConnectionsCmd; +import com.cloud.api.commands.ListVpnCustomerGatewaysCmd; +import com.cloud.api.commands.ListVpnGatewaysCmd; +import com.cloud.api.commands.ResetVpnConnectionCmd; +import com.cloud.api.commands.UpdateVpnCustomerGatewayCmd; +import com.cloud.exception.NetworkRuleConflictException; +import com.cloud.exception.ResourceUnavailableException; +import com.cloud.network.Site2SiteCustomerGateway; +import com.cloud.network.Site2SiteVpnConnection; +import com.cloud.network.Site2SiteVpnConnectionVO; +import com.cloud.network.Site2SiteVpnGateway; +import com.cloud.network.vpn.Site2SiteVpnManager; +import com.cloud.network.vpn.Site2SiteVpnService; +import com.cloud.utils.component.Manager; +import com.cloud.vm.DomainRouterVO; + +@Local(value = { Site2SiteVpnManager.class, Site2SiteVpnService.class } ) +public class MockSite2SiteVpnManagerImpl implements Site2SiteVpnManager, Site2SiteVpnService, Manager{ + + /* (non-Javadoc) + * @see com.cloud.network.vpn.Site2SiteVpnService#createVpnGateway(com.cloud.api.commands.CreateVpnGatewayCmd) + */ + @Override + public Site2SiteVpnGateway createVpnGateway(CreateVpnGatewayCmd cmd) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpn.Site2SiteVpnService#createCustomerGateway(com.cloud.api.commands.CreateVpnCustomerGatewayCmd) + */ + @Override + public Site2SiteCustomerGateway createCustomerGateway(CreateVpnCustomerGatewayCmd cmd) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpn.Site2SiteVpnService#startVpnConnection(long) + */ + @Override + public Site2SiteVpnConnection startVpnConnection(long id) throws ResourceUnavailableException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpn.Site2SiteVpnService#getVpnGateway(java.lang.Long) + */ + @Override + public Site2SiteVpnGateway getVpnGateway(Long vpnGatewayId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpn.Site2SiteVpnService#createVpnConnection(com.cloud.api.commands.CreateVpnConnectionCmd) + */ + @Override + public Site2SiteVpnConnection createVpnConnection(CreateVpnConnectionCmd cmd) throws NetworkRuleConflictException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpn.Site2SiteVpnService#deleteCustomerGateway(com.cloud.api.commands.DeleteVpnCustomerGatewayCmd) + */ + @Override + public boolean deleteCustomerGateway(DeleteVpnCustomerGatewayCmd deleteVpnCustomerGatewayCmd) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpn.Site2SiteVpnService#deleteVpnGateway(com.cloud.api.commands.DeleteVpnGatewayCmd) + */ + @Override + public boolean deleteVpnGateway(DeleteVpnGatewayCmd deleteVpnGatewayCmd) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpn.Site2SiteVpnService#deleteVpnConnection(com.cloud.api.commands.DeleteVpnConnectionCmd) + */ + @Override + public boolean deleteVpnConnection(DeleteVpnConnectionCmd deleteVpnConnectionCmd) throws ResourceUnavailableException { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpn.Site2SiteVpnService#resetVpnConnection(com.cloud.api.commands.ResetVpnConnectionCmd) + */ + @Override + public Site2SiteVpnConnection resetVpnConnection(ResetVpnConnectionCmd resetVpnConnectionCmd) throws ResourceUnavailableException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpn.Site2SiteVpnService#searchForCustomerGateways(com.cloud.api.commands.ListVpnCustomerGatewaysCmd) + */ + @Override + public List searchForCustomerGateways(ListVpnCustomerGatewaysCmd listVpnCustomerGatewaysCmd) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpn.Site2SiteVpnService#searchForVpnGateways(com.cloud.api.commands.ListVpnGatewaysCmd) + */ + @Override + public List searchForVpnGateways(ListVpnGatewaysCmd listVpnGatewaysCmd) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpn.Site2SiteVpnService#searchForVpnConnections(com.cloud.api.commands.ListVpnConnectionsCmd) + */ + @Override + public List searchForVpnConnections(ListVpnConnectionsCmd listVpnConnectionsCmd) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpn.Site2SiteVpnService#updateCustomerGateway(com.cloud.api.commands.UpdateVpnCustomerGatewayCmd) + */ + @Override + public Site2SiteCustomerGateway updateCustomerGateway(UpdateVpnCustomerGatewayCmd updateVpnCustomerGatewayCmd) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpn.Site2SiteVpnManager#cleanupVpnConnectionByVpc(long) + */ + @Override + public boolean cleanupVpnConnectionByVpc(long vpcId) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpn.Site2SiteVpnManager#cleanupVpnGatewayByVpc(long) + */ + @Override + public boolean cleanupVpnGatewayByVpc(long vpcId) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpn.Site2SiteVpnManager#markDisconnectVpnConnByVpc(long) + */ + @Override + public void markDisconnectVpnConnByVpc(long vpcId) { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.cloud.network.vpn.Site2SiteVpnManager#getConnectionsForRouter(com.cloud.vm.DomainRouterVO) + */ + @Override + public List getConnectionsForRouter(DomainRouterVO router) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpn.Site2SiteVpnManager#deleteCustomerGatewayByAccount(long) + */ + @Override + public boolean deleteCustomerGatewayByAccount(long accountId) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.utils.component.Manager#configure(java.lang.String, java.util.Map) + */ + @Override + public boolean configure(String name, Map params) throws ConfigurationException { + // TODO Auto-generated method stub + return true; + } + + /* (non-Javadoc) + * @see com.cloud.utils.component.Manager#start() + */ + @Override + public boolean start() { + // TODO Auto-generated method stub + return true; + } + + /* (non-Javadoc) + * @see com.cloud.utils.component.Manager#stop() + */ + @Override + public boolean stop() { + // TODO Auto-generated method stub + return true; + } + + /* (non-Javadoc) + * @see com.cloud.utils.component.Manager#getName() + */ + @Override + public String getName() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpn.Site2SiteVpnManager#reconnectDisconnectedVpnByVpc(java.lang.Long) + */ + @Override + public void reconnectDisconnectedVpnByVpc(Long vpcId) { + // TODO Auto-generated method stub + + } + +} diff --git a/server/test/com/cloud/vpc/MockVpcManagerImpl.java b/server/test/com/cloud/vpc/MockVpcManagerImpl.java new file mode 100644 index 00000000000..8fb2aee7c74 --- /dev/null +++ b/server/test/com/cloud/vpc/MockVpcManagerImpl.java @@ -0,0 +1,445 @@ +// 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.vpc; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.ejb.Local; +import javax.naming.ConfigurationException; + +import com.cloud.acl.ControlledEntity.ACLType; +import com.cloud.api.commands.ListPrivateGatewaysCmd; +import com.cloud.api.commands.ListStaticRoutesCmd; +import com.cloud.exception.ConcurrentOperationException; +import com.cloud.exception.InsufficientAddressCapacityException; +import com.cloud.exception.InsufficientCapacityException; +import com.cloud.exception.NetworkRuleConflictException; +import com.cloud.exception.ResourceAllocationException; +import com.cloud.exception.ResourceUnavailableException; +import com.cloud.network.IpAddress; +import com.cloud.network.Network; +import com.cloud.network.Network.Provider; +import com.cloud.network.Network.Service; +import com.cloud.network.PhysicalNetwork; +import com.cloud.network.addr.PublicIp; +import com.cloud.network.vpc.PrivateGateway; +import com.cloud.network.vpc.StaticRoute; +import com.cloud.network.vpc.Vpc; +import com.cloud.network.vpc.VpcGateway; +import com.cloud.network.vpc.VpcManager; +import com.cloud.network.vpc.VpcOffering; +import com.cloud.network.vpc.VpcService; +import com.cloud.user.Account; +import com.cloud.user.User; +import com.cloud.utils.component.ComponentLocator; +import com.cloud.utils.component.Manager; +import com.cloud.vm.DomainRouterVO; +import com.cloud.vpc.dao.MockVpcDaoImpl; + +@Local(value = { VpcManager.class, VpcService.class }) +public class MockVpcManagerImpl implements VpcManager, Manager{ + MockVpcDaoImpl _vpcDao = ComponentLocator.inject(MockVpcDaoImpl.class); + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcService#getVpcOffering(long) + */ + @Override + public VpcOffering getVpcOffering(long vpcOfferingId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcService#createVpcOffering(java.lang.String, java.lang.String, java.util.List) + */ + @Override + public VpcOffering createVpcOffering(String name, String displayText, List supportedServices) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcService#getVpc(long) + */ + @Override + public Vpc getVpc(long vpcId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcService#getActiveVpc(long) + */ + @Override + public Vpc getActiveVpc(long vpcId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcService#getVpcNetworks(long) + */ + @Override + public List getVpcNetworks(long vpcId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcService#getVpcOffSvcProvidersMap(long) + */ + @Override + public Map> getVpcOffSvcProvidersMap(long vpcOffId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcService#listVpcOfferings(java.lang.Long, java.lang.String, java.lang.String, java.util.List, java.lang.Boolean, java.lang.String, java.lang.String, java.lang.Long, java.lang.Long) + */ + @Override + public List listVpcOfferings(Long id, String name, String displayText, List supportedServicesStr, Boolean isDefault, String keyword, String state, Long startIndex, Long pageSizeVal) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcService#deleteVpcOffering(long) + */ + @Override + public boolean deleteVpcOffering(long offId) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcService#updateVpcOffering(long, java.lang.String, java.lang.String, java.lang.String) + */ + @Override + public VpcOffering updateVpcOffering(long vpcOffId, String vpcOfferingName, String displayText, String state) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcService#createVpc(long, long, long, java.lang.String, java.lang.String, java.lang.String, java.lang.String) + */ + @Override + public Vpc createVpc(long zoneId, long vpcOffId, long vpcOwnerId, String vpcName, String displayText, String cidr, String networkDomain) throws ResourceAllocationException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcService#deleteVpc(long) + */ + @Override + public boolean deleteVpc(long vpcId) throws ConcurrentOperationException, ResourceUnavailableException { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcService#updateVpc(long, java.lang.String, java.lang.String) + */ + @Override + public Vpc updateVpc(long vpcId, String vpcName, String displayText) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcService#listVpcs(java.lang.Long, java.lang.String, java.lang.String, java.util.List, java.lang.String, java.lang.Long, java.lang.String, java.lang.String, java.lang.Long, java.lang.String, java.lang.Long, java.lang.Long, java.lang.Long, java.lang.Boolean, java.lang.Boolean, java.lang.Boolean, java.util.Map, java.lang.Long) + */ + @Override + public List listVpcs(Long id, String vpcName, String displayText, List supportedServicesStr, String cidr, Long vpcOffId, String state, String accountName, Long domainId, String keyword, + Long startIndex, Long pageSizeVal, Long zoneId, Boolean isRecursive, Boolean listAll, Boolean restartRequired, Map tags, Long projectId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcService#startVpc(long, boolean) + */ + @Override + public boolean startVpc(long vpcId, boolean destroyOnFailure) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcService#shutdownVpc(long) + */ + @Override + public boolean shutdownVpc(long vpcId) throws ConcurrentOperationException, ResourceUnavailableException { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcService#restartVpc(long) + */ + @Override + public boolean restartVpc(long id) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcService#getVpcPrivateGateway(long) + */ + @Override + public PrivateGateway getVpcPrivateGateway(long id) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcService#createVpcPrivateGateway(long, java.lang.Long, java.lang.String, java.lang.String, java.lang.String, java.lang.String, long) + */ + @Override + public PrivateGateway createVpcPrivateGateway(long vpcId, Long physicalNetworkId, String vlan, String ipAddress, String gateway, String netmask, long gatewayOwnerId) throws ResourceAllocationException, + ConcurrentOperationException, InsufficientCapacityException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcService#applyVpcPrivateGateway(long, boolean) + */ + @Override + public PrivateGateway applyVpcPrivateGateway(long gatewayId, boolean destroyOnFailure) throws ConcurrentOperationException, ResourceUnavailableException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcService#deleteVpcPrivateGateway(long) + */ + @Override + public boolean deleteVpcPrivateGateway(long gatewayId) throws ConcurrentOperationException, ResourceUnavailableException { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcService#listPrivateGateway(com.cloud.api.commands.ListPrivateGatewaysCmd) + */ + @Override + public List listPrivateGateway(ListPrivateGatewaysCmd listPrivateGatewaysCmd) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcService#getStaticRoute(long) + */ + @Override + public StaticRoute getStaticRoute(long routeId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcService#applyStaticRoutes(long) + */ + @Override + public boolean applyStaticRoutes(long vpcId) throws ResourceUnavailableException { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcService#revokeStaticRoute(long) + */ + @Override + public boolean revokeStaticRoute(long routeId) throws ResourceUnavailableException { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcService#createStaticRoute(long, java.lang.String) + */ + @Override + public StaticRoute createStaticRoute(long gatewayId, String cidr) throws NetworkRuleConflictException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcService#listStaticRoutes(com.cloud.api.commands.ListStaticRoutesCmd) + */ + @Override + public List listStaticRoutes(ListStaticRoutesCmd cmd) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcService#getVpcGateway(long) + */ + @Override + public VpcGateway getVpcGateway(long id) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcService#associateIPToVpc(long, long) + */ + @Override + public IpAddress associateIPToVpc(long ipId, long vpcId) throws ResourceAllocationException, ResourceUnavailableException, InsufficientAddressCapacityException, ConcurrentOperationException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcService#updateVpcGuestNetwork(long, java.lang.String, java.lang.String, com.cloud.user.Account, com.cloud.user.User, java.lang.String, java.lang.Long, java.lang.Boolean) + */ + @Override + public Network updateVpcGuestNetwork(long networkId, String name, String displayText, Account callerAccount, User callerUser, String domainSuffix, Long ntwkOffId, Boolean changeCidr) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcManager#validateNtkwOffForVpc(long, java.lang.String, java.lang.String, com.cloud.user.Account, com.cloud.network.vpc.Vpc, java.lang.Long, java.lang.String) + */ + @Override + public void validateNtkwOffForVpc(long ntwkOffId, String cidr, String networkDomain, Account networkOwner, Vpc vpc, Long networkId, String gateway) { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcManager#getVpcsForAccount(long) + */ + @Override + public List getVpcsForAccount(long accountId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcManager#destroyVpc(com.cloud.network.vpc.Vpc) + */ + @Override + public boolean destroyVpc(Vpc vpc) throws ConcurrentOperationException, ResourceUnavailableException { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcManager#getVpcRouters(long) + */ + @Override + public List getVpcRouters(long vpcId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcManager#vpcProviderEnabledInZone(long) + */ + @Override + public boolean vpcProviderEnabledInZone(long zoneId) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcManager#getPrivateGatewayForVpc(long) + */ + @Override + public VpcGateway getPrivateGatewayForVpc(long vpcId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcManager#ipUsedInVpc(com.cloud.network.IpAddress) + */ + @Override + public boolean ipUsedInVpc(IpAddress ip) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcManager#unassignIPFromVpcNetwork(long, long) + */ + @Override + public void unassignIPFromVpcNetwork(long ipId, long networkId) { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcManager#createVpcGuestNetwork(long, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, com.cloud.user.Account, java.lang.Long, com.cloud.network.PhysicalNetwork, long, com.cloud.acl.ControlledEntity.ACLType, java.lang.Boolean, long, com.cloud.user.Account) + */ + @Override + public Network createVpcGuestNetwork(long ntwkOffId, String name, String displayText, String gateway, String cidr, String vlanId, String networkDomain, Account owner, Long domainId, PhysicalNetwork pNtwk, + long zoneId, ACLType aclType, Boolean subdomainAccess, long vpcId, Account caller) throws ConcurrentOperationException, InsufficientCapacityException, ResourceAllocationException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.VpcManager#assignSourceNatIpAddressToVpc(com.cloud.user.Account, com.cloud.network.vpc.Vpc) + */ + @Override + public PublicIp assignSourceNatIpAddressToVpc(Account owner, Vpc vpc) throws InsufficientAddressCapacityException, ConcurrentOperationException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.utils.component.Manager#configure(java.lang.String, java.util.Map) + */ + @Override + public boolean configure(String name, Map params) throws ConfigurationException { + // TODO Auto-generated method stub + return true; + } + + /* (non-Javadoc) + * @see com.cloud.utils.component.Manager#start() + */ + @Override + public boolean start() { + // TODO Auto-generated method stub + return true; + } + + /* (non-Javadoc) + * @see com.cloud.utils.component.Manager#stop() + */ + @Override + public boolean stop() { + // TODO Auto-generated method stub + return true; + } + + /* (non-Javadoc) + * @see com.cloud.utils.component.Manager#getName() + */ + @Override + public String getName() { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/server/test/com/cloud/vpc/VpcApiUnitTest.java b/server/test/com/cloud/vpc/VpcApiUnitTest.java new file mode 100644 index 00000000000..dcec8531fa3 --- /dev/null +++ b/server/test/com/cloud/vpc/VpcApiUnitTest.java @@ -0,0 +1,289 @@ +// 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.vpc; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.TestCase; + +import org.apache.log4j.Logger; +import org.junit.Before; + +import com.cloud.configuration.dao.ConfigurationDaoImpl; +import com.cloud.configuration.dao.ResourceCountDaoImpl; +import com.cloud.dc.dao.VlanDaoImpl; +import com.cloud.exception.InvalidParameterValueException; +import com.cloud.network.Network.Service; +import com.cloud.network.dao.FirewallRulesDaoImpl; +import com.cloud.network.dao.IPAddressDaoImpl; +import com.cloud.network.dao.PhysicalNetworkDaoImpl; +import com.cloud.network.element.VpcVirtualRouterElement; +import com.cloud.network.vpc.Vpc; +import com.cloud.network.vpc.VpcManager; +import com.cloud.network.vpc.VpcManagerImpl; +import com.cloud.network.vpc.Dao.PrivateIpDaoImpl; +import com.cloud.network.vpc.Dao.StaticRouteDaoImpl; +import com.cloud.network.vpc.Dao.VpcGatewayDaoImpl; +import com.cloud.network.vpc.Dao.VpcOfferingDaoImpl; +import com.cloud.server.ManagementService; +import com.cloud.tags.dao.ResourceTagsDaoImpl; +import com.cloud.user.AccountVO; +import com.cloud.user.MockAccountManagerImpl; +import com.cloud.user.dao.AccountDaoImpl; +import com.cloud.utils.component.Adapter; +import com.cloud.utils.component.ComponentLocator; +import com.cloud.utils.component.MockComponentLocator; +import com.cloud.vm.dao.DomainRouterDaoImpl; +import com.cloud.vpc.dao.MockNetworkDaoImpl; +import com.cloud.vpc.dao.MockNetworkOfferingDaoImpl; +import com.cloud.vpc.dao.MockNetworkOfferingServiceMapDaoImpl; +import com.cloud.vpc.dao.MockNetworkServiceMapDaoImpl; +import com.cloud.vpc.dao.MockVpcDaoImpl; +import com.cloud.vpc.dao.MockVpcOfferingDaoImpl; +import com.cloud.vpc.dao.MockVpcOfferingServiceMapDaoImpl; + +public class VpcApiUnitTest extends TestCase{ + private static final Logger s_logger = Logger.getLogger(VpcApiUnitTest.class); + MockComponentLocator _locator; + VpcManager _vpcService; + + @Override + @Before + public void setUp() throws Exception { + _locator = new MockComponentLocator(ManagementService.Name); + _locator.addDao("VpcDao", MockVpcDaoImpl.class); + _locator.addDao("VpcOfferingDao", VpcOfferingDaoImpl.class); + _locator.addDao("ConfigurationDao", ConfigurationDaoImpl.class); + _locator.addDao("NetworkDao", MockNetworkDaoImpl.class); + _locator.addDao("IPAddressDao", IPAddressDaoImpl.class); + _locator.addDao("DomainRouterDao", DomainRouterDaoImpl.class); + _locator.addDao("VpcGatewayDao", VpcGatewayDaoImpl.class); + _locator.addDao("PrivateIpDao", PrivateIpDaoImpl.class); + _locator.addDao("StaticRouteDao", StaticRouteDaoImpl.class); + _locator.addDao("NetworkOfferingServiceMapDao", MockNetworkOfferingServiceMapDaoImpl.class); + _locator.addDao("VpcOfferingServiceMapDao", MockVpcOfferingServiceMapDaoImpl.class); + _locator.addDao("PhysicalNetworkDao", PhysicalNetworkDaoImpl.class); + _locator.addDao("ResourceTagDao", ResourceTagsDaoImpl.class); + _locator.addDao("FirewallRulesDao", FirewallRulesDaoImpl.class); + _locator.addDao("VlanDao", VlanDaoImpl.class); + _locator.addDao("AccountDao", AccountDaoImpl.class); + _locator.addDao("ResourceCountDao", ResourceCountDaoImpl.class); + _locator.addDao("NetworkOfferingDao", MockNetworkOfferingDaoImpl.class); + _locator.addDao("NetworkServiceMapDao", MockNetworkServiceMapDaoImpl.class); + _locator.addDao("VpcOfferingDao", MockVpcOfferingDaoImpl.class); + + _locator.addManager("ConfigService", MockConfigurationManagerImpl.class); + _locator.addManager("vpc manager", VpcManagerImpl.class); + _locator.addManager("account manager", MockAccountManagerImpl.class); + _locator.addManager("network manager", MockNetworkManagerImpl.class); + _locator.addManager("Site2SiteVpnManager", MockSite2SiteVpnManagerImpl.class); + _locator.addManager("ResourceLimitService", MockResourceLimitManagerImpl.class); + + _locator.makeActive(null); + + _vpcService = ComponentLocator.inject(VpcManagerImpl.class); + } + + public void test() { + s_logger.debug("Starting test for VpcService interface"); + //Vpc service methods + getActiveVpc(); + deleteVpc(); + + //Vpc manager methods + validateNtwkOffForVpc(); + //destroyVpc(); + + } + + protected void deleteVpc() { + //delete existing offering + boolean result = false; + String msg = null; + try { + List svcs = new ArrayList(); + svcs.add(Service.SourceNat.getName()); + result = _vpcService.deleteVpc(1); + } catch (Exception ex) { + msg = ex.getMessage(); + } finally { + if (result) { + s_logger.debug("Delete vpc: Test passed, vpc is deleted"); + } else { + s_logger.error("Delete vpc: TEST FAILED, vpc failed to delete " + msg); + } + } + + //delete non-existing offering + result = false; + msg = null; + try { + List svcs = new ArrayList(); + svcs.add(Service.SourceNat.getName()); + result = _vpcService.deleteVpc(100); + } catch (Exception ex) { + msg = ex.getMessage(); + } finally { + if (!result) { + s_logger.debug("Delete vpc: Test passed, non existing vpc failed to delete "); + } else { + s_logger.error("Delete vpc: TEST FAILED, true is returned when try to delete non existing vpc"); + } + } + } + + protected void getActiveVpc() { + //test for active vpc + boolean result = false; + String msg = null; + Vpc vpc = null; + try { + List svcs = new ArrayList(); + svcs.add(Service.SourceNat.getName()); + vpc = _vpcService.getActiveVpc(1); + if (vpc != null) { + result = true; + } + } catch (Exception ex) { + msg = ex.getMessage(); + } finally { + if (result) { + s_logger.debug("Get active Vpc: Test passed, active vpc is returned"); + } else { + s_logger.error("Get active Vpc: TEST FAILED, active vpc is not returned " + msg); + } + } + + //test for inactive vpc + result = false; + msg = null; + vpc = null; + try { + List svcs = new ArrayList(); + svcs.add(Service.SourceNat.getName()); + vpc = _vpcService.getActiveVpc(2); + if (vpc != null) { + result = true; + } + } catch (Exception ex) { + msg = ex.getMessage(); + } finally { + if (!result) { + s_logger.debug("Get active Vpc: Test passed, no vpc is returned"); + } else { + s_logger.error("Get active Vpc: TEST FAILED, non active vpc is returned"); + } + } + } + + protected void destroyVpc() { + try { + _vpcService.destroyVpc(_vpcService.getVpc(1)); + } catch (Exception ex) { + s_logger.error("Destroy VPC TEST FAILED due to exc ", ex); + } + } + + protected void validateNtwkOffForVpc() { + //validate network offering + //1) correct network offering + boolean result = false; + try { + _vpcService.validateNtkwOffForVpc(1, "0.0.0.0", "111-", new AccountVO(), _vpcService.getVpc(1), 2L, "10.1.1.1"); + result = true; + s_logger.debug("Validate network offering: Test passed: the offering is valid for vpc creation"); + } catch (Exception ex) { + s_logger.error("Validate network offering: TEST FAILED due to exc ", ex); + } + + //2) invalid offering - source nat is not included + result = false; + String msg = null; + try { + _vpcService.validateNtkwOffForVpc(2, "0.0.0.0", "111-", new AccountVO(), _vpcService.getVpc(1), 2L, "10.1.1.1"); + result = true; + } catch (InvalidParameterValueException ex) { + msg = ex.getMessage(); + } finally { + if (!result) { + s_logger.debug("Validate network offering: Test passed: " + msg); + } else { + s_logger.error("Validate network offering: TEST FAILED, can't use network offering without SourceNat service"); + } + } + + //3) invalid offering - conserve mode is off + result = false; + msg = null; + try { + _vpcService.validateNtkwOffForVpc(3, "0.0.0.0", "111-", new AccountVO(), _vpcService.getVpc(1), 2L, "10.1.1.1"); + result = true; + } catch (InvalidParameterValueException ex) { + msg = ex.getMessage(); + } finally { + if (!result) { + s_logger.debug("Validate network offering: Test passed: " + msg); + } else { + s_logger.error("Validate network offering: TEST FAILED, can't use network offering without conserve mode = true"); + } + } + + //4) invalid offering - guest type shared + result = false; + try { + _vpcService.validateNtkwOffForVpc(4, "0.0.0.0", "111-", new AccountVO(), _vpcService.getVpc(1), 2L, "10.1.1.1"); + result = true; + } catch (InvalidParameterValueException ex) { + msg = ex.getMessage(); + } finally { + if (!result) { + s_logger.debug("Validate network offering: Test passed: " + msg); + } else { + s_logger.error("Validate network offering: TEST FAILED, can't use network offering with guest type = Shared"); + } + } + + //5) Invalid offering - no redundant router support + result = false; + try { + _vpcService.validateNtkwOffForVpc(5, "0.0.0.0", "111-", new AccountVO(), _vpcService.getVpc(1), 2L, "10.1.1.1"); + result = true; + } catch (InvalidParameterValueException ex) { + msg = ex.getMessage(); + } finally { + if (!result) { + s_logger.debug("Validate network offering: Test passed: " + msg); + } else { + s_logger.error("TEST FAILED, can't use network offering with guest type = Shared"); + } + } + + //6) Only one network in the VPC can support LB service - negative scenario + result = false; + try { + _vpcService.validateNtkwOffForVpc(6, "0.0.0.0", "111-", new AccountVO(), _vpcService.getVpc(1), 2L, "10.1.1.1"); + result = true; + s_logger.debug("Validate network offering: Test passed: the offering is valid for vpc creation"); + } catch (InvalidParameterValueException ex) { + msg = ex.getMessage(); + } finally { + if (!result) { + s_logger.debug("Test passed: " + msg); + } else { + s_logger.error("Validate network offering: TEST FAILED, can't use network offering with guest type = Shared"); + } + } + } + +} diff --git a/server/test/com/cloud/vpc/dao/MockConfigurationDaoImpl.java b/server/test/com/cloud/vpc/dao/MockConfigurationDaoImpl.java new file mode 100644 index 00000000000..0cac4b9dfde --- /dev/null +++ b/server/test/com/cloud/vpc/dao/MockConfigurationDaoImpl.java @@ -0,0 +1,106 @@ +// 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.vpc.dao; + +import java.util.HashMap; +import java.util.Map; + +import javax.ejb.Local; + +import com.cloud.configuration.ConfigurationVO; +import com.cloud.configuration.dao.ConfigurationDao; +import com.cloud.utils.db.GenericDaoBase; + +@Local(value={ConfigurationDao.class}) +public class MockConfigurationDaoImpl extends GenericDaoBase implements ConfigurationDao{ + + /* (non-Javadoc) + * @see com.cloud.configuration.dao.ConfigurationDao#getConfiguration(java.lang.String, java.util.Map) + */ + @Override + public Map getConfiguration(String instance, Map params) { + return new HashMap(); + } + + /* (non-Javadoc) + * @see com.cloud.configuration.dao.ConfigurationDao#getConfiguration(java.util.Map) + */ + @Override + public Map getConfiguration(Map params) { + return new HashMap(); + } + + /* (non-Javadoc) + * @see com.cloud.configuration.dao.ConfigurationDao#getConfiguration() + */ + @Override + public Map getConfiguration() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.dao.ConfigurationDao#update(java.lang.String, java.lang.String) + */ + @Override + public boolean update(String name, String value) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.dao.ConfigurationDao#getValue(java.lang.String) + */ + @Override + public String getValue(String name) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.dao.ConfigurationDao#getValueAndInitIfNotExist(java.lang.String, java.lang.String, java.lang.String) + */ + @Override + public String getValueAndInitIfNotExist(String name, String category, String initValue) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.dao.ConfigurationDao#isPremium() + */ + @Override + public boolean isPremium() { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.dao.ConfigurationDao#findByName(java.lang.String) + */ + @Override + public ConfigurationVO findByName(String name) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.configuration.dao.ConfigurationDao#update(java.lang.String, java.lang.String, java.lang.String) + */ + @Override + public boolean update(String name, String category, String value) { + // TODO Auto-generated method stub + return false; + } + +} diff --git a/server/test/com/cloud/vpc/dao/MockNetworkDaoImpl.java b/server/test/com/cloud/vpc/dao/MockNetworkDaoImpl.java new file mode 100644 index 00000000000..970a5d9ba57 --- /dev/null +++ b/server/test/com/cloud/vpc/dao/MockNetworkDaoImpl.java @@ -0,0 +1,341 @@ +// 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.vpc.dao; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import javax.ejb.Local; + +import com.cloud.network.Network.GuestType; +import com.cloud.network.NetworkAccountVO; +import com.cloud.network.NetworkVO; +import com.cloud.network.Networks.TrafficType; +import com.cloud.network.dao.NetworkDao; +import com.cloud.utils.db.DB; +import com.cloud.utils.db.GenericDaoBase; +import com.cloud.utils.db.SearchBuilder; + +@Local(value = NetworkDao.class) +@DB(txn = false) +public class MockNetworkDaoImpl extends GenericDaoBase implements NetworkDao{ + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#listByOwner(long) + */ + @Override + public List listByOwner(long ownerId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#listBy(long, long, long) + */ + @Override + public List listBy(long accountId, long offeringId, long dataCenterId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#listBy(long, long, java.lang.String, boolean) + */ + @Override + public List listBy(long accountId, long dataCenterId, String cidr, boolean skipVpc) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#listByZoneAndGuestType(long, long, com.cloud.network.Network.GuestType, java.lang.Boolean) + */ + @Override + public List listByZoneAndGuestType(long accountId, long dataCenterId, GuestType type, Boolean isSystem) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#persist(com.cloud.network.NetworkVO, boolean, java.util.Map) + */ + @Override + public NetworkVO persist(NetworkVO network, boolean gc, Map serviceProviderMap) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#createSearchBuilderForAccount() + */ + @Override + public SearchBuilder createSearchBuilderForAccount() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#getNetworksForOffering(long, long, long) + */ + @Override + public List getNetworksForOffering(long offeringId, long dataCenterId, long accountId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#getNextAvailableMacAddress(long) + */ + @Override + public String getNextAvailableMacAddress(long networkConfigId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#listBy(long, long) + */ + @Override + public List listBy(long accountId, long networkId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#countByZoneAndUri(long, java.lang.String) + */ + @Override + public long countByZoneAndUri(long zoneId, String broadcastUri) { + // TODO Auto-generated method stub + return 0; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#countByZoneUriAndGuestType(long, java.lang.String, com.cloud.network.Network.GuestType) + */ + @Override + public long countByZoneUriAndGuestType(long zoneId, String broadcastUri, GuestType guestType) { + // TODO Auto-generated method stub + return 0; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#listByZone(long) + */ + @Override + public List listByZone(long zoneId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#changeActiveNicsBy(long, int) + */ + @Override + public void changeActiveNicsBy(long networkId, int nicsCount) { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#getActiveNicsIn(long) + */ + @Override + public int getActiveNicsIn(long networkId) { + // TODO Auto-generated method stub + return 0; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#findNetworksToGarbageCollect() + */ + @Override + public List findNetworksToGarbageCollect() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#clearCheckForGc(long) + */ + @Override + public void clearCheckForGc(long networkId) { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#listByZoneSecurityGroup(java.lang.Long) + */ + @Override + public List listByZoneSecurityGroup(Long zoneId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#addDomainToNetwork(long, long, java.lang.Boolean) + */ + @Override + public void addDomainToNetwork(long networkId, long domainId, Boolean subdomainAccess) { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#listByPhysicalNetwork(long) + */ + @Override + public List listByPhysicalNetwork(long physicalNetworkId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#listSecurityGroupEnabledNetworks() + */ + @Override + public List listSecurityGroupEnabledNetworks() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#listByPhysicalNetworkTrafficType(long, com.cloud.network.Networks.TrafficType) + */ + @Override + public List listByPhysicalNetworkTrafficType(long physicalNetworkId, TrafficType trafficType) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#listBy(long, long, com.cloud.network.Network.GuestType, com.cloud.network.Networks.TrafficType) + */ + @Override + public List listBy(long accountId, long dataCenterId, GuestType type, TrafficType trafficType) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#listByPhysicalNetworkAndProvider(long, java.lang.String) + */ + @Override + public List listByPhysicalNetworkAndProvider(long physicalNetworkId, String providerName) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#persistNetworkServiceProviders(long, java.util.Map) + */ + @Override + public void persistNetworkServiceProviders(long networkId, Map serviceProviderMap) { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#update(java.lang.Long, com.cloud.network.NetworkVO, java.util.Map) + */ + @Override + public boolean update(Long networkId, NetworkVO network, Map serviceProviderMap) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#listByZoneAndTrafficType(long, com.cloud.network.Networks.TrafficType) + */ + @Override + public List listByZoneAndTrafficType(long zoneId, TrafficType trafficType) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#setCheckForGc(long) + */ + @Override + public void setCheckForGc(long networkId) { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#getNetworkCountByNetworkOffId(long) + */ + @Override + public int getNetworkCountByNetworkOffId(long networkOfferingId) { + // TODO Auto-generated method stub + return 0; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#countNetworksUserCanCreate(long) + */ + @Override + public long countNetworksUserCanCreate(long ownerId) { + // TODO Auto-generated method stub + return 0; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#listSourceNATEnabledNetworks(long, long, com.cloud.network.Network.GuestType) + */ + @Override + public List listSourceNATEnabledNetworks(long accountId, long dataCenterId, GuestType type) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#getNetworkCountByVpcId(long) + */ + @Override + public int getNetworkCountByVpcId(long vpcId) { + // TODO Auto-generated method stub + return 0; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#listByVpc(long) + */ + @Override + public List listByVpc(long vpcId) { + List networks = new ArrayList(); + networks.add(new NetworkVO()); + return networks; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#getPrivateNetwork(java.lang.String, java.lang.String, long, long) + */ + @Override + public NetworkVO getPrivateNetwork(String broadcastUri, String cidr, long accountId, long zoneId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkDao#countVpcNetworks(long) + */ + @Override + public long countVpcNetworks(long vpcId) { + // TODO Auto-generated method stub + return 0; + } + +} diff --git a/server/test/com/cloud/vpc/dao/MockNetworkOfferingDaoImpl.java b/server/test/com/cloud/vpc/dao/MockNetworkOfferingDaoImpl.java new file mode 100644 index 00000000000..dfeadf67a03 --- /dev/null +++ b/server/test/com/cloud/vpc/dao/MockNetworkOfferingDaoImpl.java @@ -0,0 +1,148 @@ +// 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.vpc.dao; + +import java.lang.reflect.Field; +import java.util.List; + +import javax.ejb.Local; + +import org.apache.log4j.Logger; + +import com.cloud.network.Network; +import com.cloud.network.Network.GuestType; +import com.cloud.network.Networks.TrafficType; +import com.cloud.offering.NetworkOffering; +import com.cloud.offering.NetworkOffering.Availability; +import com.cloud.offering.NetworkOffering.State; +import com.cloud.offerings.NetworkOfferingVO; +import com.cloud.offerings.dao.NetworkOfferingDao; +import com.cloud.utils.db.DB; +import com.cloud.utils.db.GenericDaoBase; + +@Local(value = NetworkOfferingDao.class) +@DB(txn = false) +public class MockNetworkOfferingDaoImpl extends GenericDaoBase implements NetworkOfferingDao { + private static final Logger s_logger = Logger.getLogger(MockNetworkOfferingDaoImpl.class); + + /* (non-Javadoc) + * @see com.cloud.offerings.dao.NetworkOfferingDao#findByUniqueName(java.lang.String) + */ + @Override + public NetworkOfferingVO findByUniqueName(String uniqueName) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.offerings.dao.NetworkOfferingDao#persistDefaultNetworkOffering(com.cloud.offerings.NetworkOfferingVO) + */ + @Override + public NetworkOfferingVO persistDefaultNetworkOffering(NetworkOfferingVO offering) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.offerings.dao.NetworkOfferingDao#listSystemNetworkOfferings() + */ + @Override + public List listSystemNetworkOfferings() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.offerings.dao.NetworkOfferingDao#listByAvailability(com.cloud.offering.NetworkOffering.Availability, boolean) + */ + @Override + public List listByAvailability(Availability availability, boolean isSystem) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.offerings.dao.NetworkOfferingDao#getOfferingIdsToUpgradeFrom(com.cloud.offering.NetworkOffering) + */ + @Override + public List getOfferingIdsToUpgradeFrom(NetworkOffering originalOffering) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.offerings.dao.NetworkOfferingDao#listByTrafficTypeGuestTypeAndState(com.cloud.offering.NetworkOffering.State, com.cloud.network.Networks.TrafficType, com.cloud.network.Network.GuestType) + */ + @Override + public List listByTrafficTypeGuestTypeAndState(State state, TrafficType trafficType, GuestType type) { + // TODO Auto-generated method stub + return null; + } + + + @Override + public NetworkOfferingVO findById(Long id) { + NetworkOfferingVO vo = null; + if (id.longValue() == 1) { + //network offering valid for vpc + vo = new NetworkOfferingVO("vpc", "vpc", TrafficType.Guest, false, true, null, null, false, + Availability.Optional, null, Network.GuestType.Isolated, false, false); + } else if (id.longValue() == 2) { + //invalid offering - source nat is not included + vo = new NetworkOfferingVO("vpc", "vpc", TrafficType.Guest, false, true, null, null, false, + Availability.Optional, null, Network.GuestType.Isolated, false, false); + } else if (id.longValue() == 3) { + //network offering invalid for vpc (conserve mode off) + vo = new NetworkOfferingVO("non vpc", "non vpc", TrafficType.Guest, false, true, null, null, false, + Availability.Optional, null, Network.GuestType.Isolated, true, false); + } else if (id.longValue() == 4) { + //network offering invalid for vpc (Shared) + vo = new NetworkOfferingVO("non vpc", "non vpc", TrafficType.Guest, false, true, null, null, false, + Availability.Optional, null, Network.GuestType.Shared, false, false); + } else if (id.longValue() == 5) { + //network offering invalid for vpc (has redundant router) + vo = new NetworkOfferingVO("vpc", "vpc", TrafficType.Guest, false, true, null, null, false, + Availability.Optional, null, Network.GuestType.Isolated, false, false); + vo.setRedundantRouter(true); + } else if (id.longValue() == 6) { + //network offering invalid for vpc (has lb service) + vo = new NetworkOfferingVO("vpc", "vpc", TrafficType.Guest, false, true, null, null, false, + Availability.Optional, null, Network.GuestType.Isolated, false, false); + } + + if (vo != null) { + vo = setId(vo, id); + } + + return vo; + } + + private NetworkOfferingVO setId(NetworkOfferingVO vo, long id) { + NetworkOfferingVO voToReturn = vo; + Class c = voToReturn.getClass(); + try { + Field f = c.getDeclaredField("id"); + f.setAccessible(true); + f.setLong(voToReturn, id); + } catch (NoSuchFieldException ex) { + s_logger.warn(ex); + return null; + } catch (IllegalAccessException ex) { + s_logger.warn(ex); + return null; + } + + return voToReturn; + } + +} diff --git a/server/test/com/cloud/vpc/dao/MockNetworkOfferingServiceMapDaoImpl.java b/server/test/com/cloud/vpc/dao/MockNetworkOfferingServiceMapDaoImpl.java new file mode 100644 index 00000000000..2e388c2e447 --- /dev/null +++ b/server/test/com/cloud/vpc/dao/MockNetworkOfferingServiceMapDaoImpl.java @@ -0,0 +1,35 @@ +// 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.vpc.dao; + +import javax.ejb.Local; + +import com.cloud.network.Network.Service; +import com.cloud.offerings.dao.NetworkOfferingServiceMapDao; +import com.cloud.offerings.dao.NetworkOfferingServiceMapDaoImpl; +import com.cloud.utils.db.DB; + +@Local(value = NetworkOfferingServiceMapDao.class) +@DB(txn = false) +public class MockNetworkOfferingServiceMapDaoImpl extends NetworkOfferingServiceMapDaoImpl{ + + @Override + public boolean areServicesSupportedByNetworkOffering(long networkOfferingId, Service... services) { + if (services.length > 0 && services[0] == Service.SourceNat && networkOfferingId != 2) { + return true; + } else if (services.length > 0 && services[0] == Service.Lb && networkOfferingId == 6) { + return true; + } + return false; + } +} diff --git a/server/test/com/cloud/vpc/dao/MockNetworkServiceMapDaoImpl.java b/server/test/com/cloud/vpc/dao/MockNetworkServiceMapDaoImpl.java new file mode 100644 index 00000000000..e66aa5b8913 --- /dev/null +++ b/server/test/com/cloud/vpc/dao/MockNetworkServiceMapDaoImpl.java @@ -0,0 +1,94 @@ +// 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.vpc.dao; + +import java.util.List; + +import javax.ejb.Local; + +import com.cloud.network.Network.Provider; +import com.cloud.network.Network.Service; +import com.cloud.network.NetworkServiceMapVO; +import com.cloud.network.dao.NetworkServiceMapDao; +import com.cloud.utils.db.DB; +import com.cloud.utils.db.GenericDaoBase; + +@Local(value = NetworkServiceMapDao.class) +@DB(txn = false) +public class MockNetworkServiceMapDaoImpl extends GenericDaoBase implements NetworkServiceMapDao{ + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkServiceMapDao#areServicesSupportedInNetwork(long, com.cloud.network.Network.Service[]) + */ + @Override + public boolean areServicesSupportedInNetwork(long networkId, Service... services) { + if (services.length > 0 && services[0] == Service.Lb) { + return true; + } + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkServiceMapDao#canProviderSupportServiceInNetwork(long, com.cloud.network.Network.Service, com.cloud.network.Network.Provider) + */ + @Override + public boolean canProviderSupportServiceInNetwork(long networkId, Service service, Provider provider) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkServiceMapDao#getServicesInNetwork(long) + */ + @Override + public List getServicesInNetwork(long networkId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkServiceMapDao#getProviderForServiceInNetwork(long, com.cloud.network.Network.Service) + */ + @Override + public String getProviderForServiceInNetwork(long networkid, Service service) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkServiceMapDao#deleteByNetworkId(long) + */ + @Override + public void deleteByNetworkId(long networkId) { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkServiceMapDao#getDistinctProviders(long) + */ + @Override + public List getDistinctProviders(long networkId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.dao.NetworkServiceMapDao#isProviderForNetwork(long, com.cloud.network.Network.Provider) + */ + @Override + public String isProviderForNetwork(long networkId, Provider provider) { + // TODO Auto-generated method stub + return null; + } +} diff --git a/server/test/com/cloud/vpc/dao/MockVpcDaoImpl.java b/server/test/com/cloud/vpc/dao/MockVpcDaoImpl.java new file mode 100644 index 00000000000..7579ff73826 --- /dev/null +++ b/server/test/com/cloud/vpc/dao/MockVpcDaoImpl.java @@ -0,0 +1,126 @@ +// 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.vpc.dao; + +import java.lang.reflect.Field; +import java.util.List; + +import javax.ejb.Local; + +import org.apache.log4j.Logger; + +import com.cloud.network.vpc.Vpc; +import com.cloud.network.vpc.Vpc.State; +import com.cloud.network.vpc.VpcVO; +import com.cloud.network.vpc.Dao.VpcDao; +import com.cloud.utils.db.DB; +import com.cloud.utils.db.GenericDaoBase; + +@Local(value = VpcDao.class) +@DB(txn = false) +public class MockVpcDaoImpl extends GenericDaoBase implements VpcDao{ + private static final Logger s_logger = Logger.getLogger(MockNetworkOfferingDaoImpl.class); + + + /* (non-Javadoc) + * @see com.cloud.network.vpc.Dao.VpcDao#getVpcCountByOfferingId(long) + */ + @Override + public int getVpcCountByOfferingId(long offId) { + return 100; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.Dao.VpcDao#getActiveVpcById(long) + */ + @Override + public Vpc getActiveVpcById(long vpcId) { + Vpc vpc = findById(vpcId); + if (vpc != null && vpc.getState() == Vpc.State.Enabled) { + return vpc; + } + + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.Dao.VpcDao#listByAccountId(long) + */ + @Override + public List listByAccountId(long accountId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.Dao.VpcDao#listInactiveVpcs() + */ + @Override + public List listInactiveVpcs() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.Dao.VpcDao#countByAccountId(long) + */ + @Override + public long countByAccountId(long accountId) { + // TODO Auto-generated method stub + return 0; + } + + @Override + public VpcVO findById(Long id) { + VpcVO vo = null; + if (id.longValue() == 1) { + vo = new VpcVO(1, "new vpc", "new vpc", 1,1 , 1, "0.0.0.0/0", "vpc domain"); + } else if (id.longValue() == 2) { + vo = new VpcVO(1, "new vpc", "new vpc", 1,1 , 1, "0.0.0.0/0", "vpc domain"); + vo.setState(State.Inactive); + } + + vo = setId(vo, id); + + return vo; + } + + private VpcVO setId(VpcVO vo, long id) { + VpcVO voToReturn = vo; + Class c = voToReturn.getClass(); + try { + Field f = c.getDeclaredField("id"); + f.setAccessible(true); + f.setLong(voToReturn, id); + } catch (NoSuchFieldException ex) { + s_logger.warn(ex); + return null; + } catch (IllegalAccessException ex) { + s_logger.warn(ex); + return null; + } + + return voToReturn; + } + + @Override + public boolean remove(Long id) { + return true; + } + + @Override + public boolean update(Long id, VpcVO vo) { + return true; + } + +} diff --git a/server/test/com/cloud/vpc/dao/MockVpcOfferingDaoImpl.java b/server/test/com/cloud/vpc/dao/MockVpcOfferingDaoImpl.java new file mode 100644 index 00000000000..52acf82d2cc --- /dev/null +++ b/server/test/com/cloud/vpc/dao/MockVpcOfferingDaoImpl.java @@ -0,0 +1,39 @@ +// 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.vpc.dao; + +import javax.ejb.Local; + +import com.cloud.network.vpc.VpcOfferingVO; +import com.cloud.network.vpc.Dao.VpcOfferingDao; +import com.cloud.utils.db.DB; +import com.cloud.utils.db.GenericDaoBase; + +@Local(value = VpcOfferingDao.class) +@DB(txn = false) +public class MockVpcOfferingDaoImpl extends GenericDaoBase implements VpcOfferingDao{ + + /* (non-Javadoc) + * @see com.cloud.network.vpc.Dao.VpcOfferingDao#findByUniqueName(java.lang.String) + */ + @Override + public VpcOfferingVO findByUniqueName(String uniqueName) { + return new VpcOfferingVO(); + } + + @Override + public VpcOfferingVO persist(VpcOfferingVO vo) { + return vo; + } + +} diff --git a/server/test/com/cloud/vpc/dao/MockVpcOfferingServiceMapDaoImpl.java b/server/test/com/cloud/vpc/dao/MockVpcOfferingServiceMapDaoImpl.java new file mode 100644 index 00000000000..3677a16038b --- /dev/null +++ b/server/test/com/cloud/vpc/dao/MockVpcOfferingServiceMapDaoImpl.java @@ -0,0 +1,69 @@ +// 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.vpc.dao; + +import java.util.List; + +import javax.ejb.Local; + +import com.cloud.network.Network.Service; +import com.cloud.network.vpc.VpcOfferingServiceMapVO; +import com.cloud.network.vpc.Dao.VpcOfferingServiceMapDao; +import com.cloud.utils.db.DB; +import com.cloud.utils.db.GenericDaoBase; + +@Local(value = VpcOfferingServiceMapDao.class) +@DB(txn = false) +public class MockVpcOfferingServiceMapDaoImpl extends GenericDaoBase implements VpcOfferingServiceMapDao{ + + /* (non-Javadoc) + * @see com.cloud.network.vpc.Dao.VpcOfferingServiceMapDao#listByVpcOffId(long) + */ + @Override + public List listByVpcOffId(long vpcOffId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.Dao.VpcOfferingServiceMapDao#areServicesSupportedByNetworkOffering(long, com.cloud.network.Network.Service[]) + */ + @Override + public boolean areServicesSupportedByNetworkOffering(long networkOfferingId, Service[] services) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.Dao.VpcOfferingServiceMapDao#listServicesForVpcOffering(long) + */ + @Override + public List listServicesForVpcOffering(long vpcOfferingId) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see com.cloud.network.vpc.Dao.VpcOfferingServiceMapDao#findByServiceProviderAndOfferingId(java.lang.String, java.lang.String, long) + */ + @Override + public VpcOfferingServiceMapVO findByServiceProviderAndOfferingId(String service, String provider, long vpcOfferingId) { + return new VpcOfferingServiceMapVO(); + } + + @Override + public VpcOfferingServiceMapVO persist (VpcOfferingServiceMapVO vo) { + return vo; + } + +} diff --git a/server/test/com/cloud/vpc/dao/MockVpcVirtualRouterElement.java b/server/test/com/cloud/vpc/dao/MockVpcVirtualRouterElement.java new file mode 100644 index 00000000000..91cfb9933f7 --- /dev/null +++ b/server/test/com/cloud/vpc/dao/MockVpcVirtualRouterElement.java @@ -0,0 +1,26 @@ +// 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.vpc.dao; + +import com.cloud.exception.ConcurrentOperationException; +import com.cloud.exception.ResourceUnavailableException; +import com.cloud.network.element.VpcVirtualRouterElement; +import com.cloud.network.vpc.Vpc; + +public class MockVpcVirtualRouterElement extends VpcVirtualRouterElement{ + @Override + public boolean shutdownVpc(Vpc vpc) throws ConcurrentOperationException, ResourceUnavailableException { + return true; + } + +} diff --git a/setup/db/db/schema-304to305.sql b/setup/db/db/schema-304to305.sql index 950768a4f6d..690640c7716 100755 --- a/setup/db/db/schema-304to305.sql +++ b/setup/db/db/schema-304to305.sql @@ -347,9 +347,8 @@ UPDATE `cloud`.`data_center` SET `is_local_storage_enabled` = IF ((SELECT `value ALTER TABLE `cloud`.`hypervisor_capabilities` ADD COLUMN `max_data_volumes_limit` int unsigned DEFAULT 6 COMMENT 'Max. data volumes per VM supported by hypervisor'; -SET SQL_SAFE_UPDATES=0; UPDATE `cloud`.`hypervisor_capabilities` SET `max_data_volumes_limit`=13 WHERE `hypervisor_type`='XenServer' AND (`hypervisor_version`='6.0' OR `hypervisor_version`='6.0.2'); -SET SQL_SAFE_UPDATES=1; + UPDATE `cloud`.`configuration` SET description='In second, timeout for creating volume from snapshot' WHERE name='create.volume.from.snapshot.wait'; INSERT IGNORE INTO `cloud`.`configuration` VALUES ('Account Defaults', 'DEFAULT', 'management-server', 'max.account.vpcs', '20', 'The default maximum number of vpcs that can be created for an account'); diff --git a/setup/db/templates.sql b/setup/db/templates.sql index 536d4792abc..e16e5d14592 100755 --- a/setup/db/templates.sql +++ b/setup/db/templates.sql @@ -28,7 +28,7 @@ INSERT INTO `cloud`.`vm_template` (id, unique_name, name, public, created, type, VALUES (7, 'centos53-x64', 'CentOS 5.3(64-bit) no GUI (vSphere)', 1, now(), 'BUILTIN', 0, 64, 1, 'http://download.cloud.com/releases/2.2.0/CentOS5.3-x86_64.ova', 'f6f881b7f2292948d8494db837fe0f47', 0, 'CentOS 5.3(64-bit) no GUI (vSphere)', 'OVA', 12, 1, 1, 'VMware', 1); INSERT INTO `cloud`.`vm_template` (id, unique_name, name, public, created, type, hvm, bits, account_id, url, checksum, enable_password, display_text, format, guest_os_id, featured, cross_zones, hypervisor_type) - VALUES (8, 'routing-8', 'SystemVM Template (vSphere)', 0, now(), 'SYSTEM', 0, 32, 1, 'http://download.cloud.com/templates/acton/acton-systemvm-02062012.ova', 'e72b21c9541d005600297cb92d241434', 0, 'SystemVM Template (vSphere)', 'OVA', 15, 0, 1, 'VMware'); + VALUES (8, 'routing-8', 'SystemVM Template (vSphere)', 0, now(), 'SYSTEM', 0, 32, 1, 'http://download.cloud.com/templates/burbank/burbank-systemvm-08012012.ova', '7137e453f950079ea2ba6feaafd939e8', 0, 'SystemVM Template (vSphere)', 'OVA', 15, 0, 1, 'VMware'); INSERT INTO `cloud`.`vm_template` (id, unique_name, name, public, created, type, hvm, bits, account_id, url, checksum, enable_password, display_text, format, guest_os_id, featured, cross_zones, hypervisor_type) VALUES (9, 'routing-9', 'SystemVM Template (HyperV)', 0, now(), 'SYSTEM', 0, 32, 1, 'http://download.cloud.com/templates/acton/acton-systemvm-02062012.vhd.bz2', 'f613f38c96bf039f2e5cbf92fa8ad4f8', 0, 'SystemVM Template (HyperV)', 'VHD', 15, 0, 1, 'Hyperv'); diff --git a/test/dbupgrade/cloud_304.sql b/test/dbupgrade/cloud_304.sql index 0747f5f581d..e4a0c24dd29 100644 --- a/test/dbupgrade/cloud_304.sql +++ b/test/dbupgrade/cloud_304.sql @@ -3580,7 +3580,7 @@ CREATE TABLE `template_host_ref` ( LOCK TABLES `template_host_ref` WRITE; /*!40000 ALTER TABLE `template_host_ref` DISABLE KEYS */; -INSERT INTO `template_host_ref` VALUES (1,1,9,'2012-07-19 22:20:20','2012-07-19 22:20:20',NULL,100,0,0,'DOWNLOADED',NULL,NULL,'template/tmpl/1/9/','http://download.cloud.com/templates/acton/acton-systemvm-02062012.vhd.bz2',0,0),(2,1,8,'2012-07-19 22:20:20','2012-07-19 22:20:20',NULL,100,0,0,'DOWNLOADED',NULL,NULL,'template/tmpl/1/8/','http://download.cloud.com/templates/acton/acton-systemvm-02062012.ova',0,0),(3,1,3,'2012-07-19 22:20:20','2012-07-19 22:30:12',NULL,100,757071872,757071872,'DOWNLOADED',NULL,NULL,'template/tmpl/1/3//adb6033f-c806-4884-938a-1ec398fc5ff0.qcow2','http://download.cloud.com/templates/acton/acton-systemvm-02062012.qcow2.bz2',0,0),(4,1,1,'2012-07-19 22:20:20','2012-07-19 22:30:12',NULL,100,565240320,565240320,'DOWNLOADED',NULL,NULL,'template/tmpl/1/1//a349b09e-4f8a-436a-bf3e-fc70e79c04cc.vhd','http://download.cloud.com/templates/acton/acton-systemvm-02062012.vhd.bz2',0,0),(5,1,4,'2012-07-19 22:30:12','2012-07-19 22:30:12',NULL,0,0,0,'NOT_DOWNLOADED',NULL,NULL,NULL,'http://download.cloud.com/releases/2.2.0/eec2209b-9875-3c8d-92be-c001bd8a0faf.qcow2.bz2',0,0),(6,1,5,'2012-07-19 22:30:12','2012-07-19 22:47:50','ebdb6d62-aa39-4d43-b6c4-dc9740d5e31e',100,21474836480,1758786048,'DOWNLOADED','Install completed successfully at 7/19/12 4:39 PM','/mnt/SecStorage/60b4c411-98da-336f-a7f0-84a3e1c4c218/template/tmpl/1/5/dnld1522056867875408327tmp_','template/tmpl//1/5//facf39f0-4874-3086-85af-bafbed76a2c0.vhd','http://download.cloud.com/templates/builtin/centos56-x86_64.vhd.bz2',0,0),(7,1,7,'2012-07-19 22:30:12','2012-07-19 22:30:12',NULL,0,0,0,'NOT_DOWNLOADED',NULL,NULL,NULL,'http://download.cloud.com/releases/2.2.0/CentOS5.3-x86_64.ova',0,0); +INSERT INTO `template_host_ref` VALUES (1,1,9,'2012-07-19 22:20:20','2012-07-19 22:20:20',NULL,100,0,0,'DOWNLOADED',NULL,NULL,'template/tmpl/1/9/','http://download.cloud.com/templates/acton/acton-systemvm-02062012.vhd.bz2',0,0),(2,1,8,'2012-07-19 22:20:20','2012-07-19 22:20:20',NULL,100,0,0,'DOWNLOADED',NULL,NULL,'template/tmpl/1/8/','http://download.cloud.com/templates/burbank/burbank-systemvm-08012012.ova',0,0),(3,1,3,'2012-07-19 22:20:20','2012-07-19 22:30:12',NULL,100,757071872,757071872,'DOWNLOADED',NULL,NULL,'template/tmpl/1/3//adb6033f-c806-4884-938a-1ec398fc5ff0.qcow2','http://download.cloud.com/templates/acton/acton-systemvm-02062012.qcow2.bz2',0,0),(4,1,1,'2012-07-19 22:20:20','2012-07-19 22:30:12',NULL,100,565240320,565240320,'DOWNLOADED',NULL,NULL,'template/tmpl/1/1//a349b09e-4f8a-436a-bf3e-fc70e79c04cc.vhd','http://download.cloud.com/templates/acton/acton-systemvm-02062012.vhd.bz2',0,0),(5,1,4,'2012-07-19 22:30:12','2012-07-19 22:30:12',NULL,0,0,0,'NOT_DOWNLOADED',NULL,NULL,NULL,'http://download.cloud.com/releases/2.2.0/eec2209b-9875-3c8d-92be-c001bd8a0faf.qcow2.bz2',0,0),(6,1,5,'2012-07-19 22:30:12','2012-07-19 22:47:50','ebdb6d62-aa39-4d43-b6c4-dc9740d5e31e',100,21474836480,1758786048,'DOWNLOADED','Install completed successfully at 7/19/12 4:39 PM','/mnt/SecStorage/60b4c411-98da-336f-a7f0-84a3e1c4c218/template/tmpl/1/5/dnld1522056867875408327tmp_','template/tmpl//1/5//facf39f0-4874-3086-85af-bafbed76a2c0.vhd','http://download.cloud.com/templates/builtin/centos56-x86_64.vhd.bz2',0,0),(7,1,7,'2012-07-19 22:30:12','2012-07-19 22:30:12',NULL,0,0,0,'NOT_DOWNLOADED',NULL,NULL,NULL,'http://download.cloud.com/releases/2.2.0/CentOS5.3-x86_64.ova',0,0); /*!40000 ALTER TABLE `template_host_ref` ENABLE KEYS */; UNLOCK TABLES; @@ -4210,7 +4210,7 @@ CREATE TABLE `vm_template` ( LOCK TABLES `vm_template` WRITE; /*!40000 ALTER TABLE `vm_template` DISABLE KEYS */; -INSERT INTO `vm_template` VALUES (1,'routing-1','SystemVM Template (XenServer)','ea041c86-a510-4a66-af6d-94ac2261c075',0,0,'SYSTEM',0,64,'http://download.cloud.com/templates/acton/acton-systemvm-02062012.vhd.bz2','VHD','2012-07-19 15:19:39',NULL,1,'f613f38c96bf039f2e5cbf92fa8ad4f8','SystemVM Template (XenServer)',0,0,15,1,0,1,0,'XenServer',NULL,NULL,0),(2,'centos53-x86_64','CentOS 5.3(64-bit) no GUI (XenServer)','77783498-a5f3-411f-be92-8965d06cc968',1,1,'BUILTIN',0,64,'http://download.cloud.com/templates/builtin/f59f18fb-ae94-4f97-afd2-f84755767aca.vhd.bz2','VHD','2012-07-19 15:19:39','2012-07-19 15:19:39',1,'b63d854a9560c013142567bbae8d98cf','CentOS 5.3(64-bit) no GUI (XenServer)',0,0,12,1,0,1,1,'XenServer',NULL,NULL,0),(3,'routing-3','SystemVM Template (KVM)','af37c452-9d00-4f48-80f1-1d4bd773c37e',0,0,'SYSTEM',0,64,'http://download.cloud.com/templates/acton/acton-systemvm-02062012.qcow2.bz2','QCOW2','2012-07-19 15:19:39',NULL,1,'2755de1f9ef2ce4d6f2bee2efbb4da92','SystemVM Template (KVM)',0,0,15,1,0,1,0,'KVM',NULL,NULL,0),(4,'centos55-x86_64','CentOS 5.5(64-bit) no GUI (KVM)','1f3f38ea-e0ee-4f21-92ab-27ca5949538e',1,1,'BUILTIN',0,64,'http://download.cloud.com/releases/2.2.0/eec2209b-9875-3c8d-92be-c001bd8a0faf.qcow2.bz2','QCOW2','2012-07-19 15:19:39',NULL,1,'ed0e788280ff2912ea40f7f91ca7a249','CentOS 5.5(64-bit) no GUI (KVM)',0,0,112,1,0,1,1,'KVM',NULL,NULL,0),(5,'centos56-x86_64-xen','CentOS 5.6(64-bit) no GUI (XenServer)','14a118d5-5803-48c1-82c9-85882d1ff97c',1,1,'BUILTIN',0,64,'http://download.cloud.com/templates/builtin/centos56-x86_64.vhd.bz2','VHD','2012-07-19 15:19:39',NULL,1,'905cec879afd9c9d22ecc8036131a180','CentOS 5.6(64-bit) no GUI (XenServer)',0,0,12,1,0,1,1,'XenServer',NULL,NULL,0),(7,'centos53-x64','CentOS 5.3(64-bit) no GUI (vSphere)','4801c38d-16f1-494c-ad95-2f7c89bac8e2',1,1,'BUILTIN',0,64,'http://download.cloud.com/releases/2.2.0/CentOS5.3-x86_64.ova','OVA','2012-07-19 15:19:39',NULL,1,'f6f881b7f2292948d8494db837fe0f47','CentOS 5.3(64-bit) no GUI (vSphere)',0,0,12,1,0,1,1,'VMware',NULL,NULL,0),(8,'routing-8','SystemVM Template (vSphere)','5a4a9ab4-7cdd-4d4a-b3ff-b79ae165df96',0,0,'SYSTEM',0,32,'http://download.cloud.com/templates/acton/acton-systemvm-02062012.ova','OVA','2012-07-19 15:19:39',NULL,1,'e72b21c9541d005600297cb92d241434','SystemVM Template (vSphere)',0,0,15,1,0,1,0,'VMware',NULL,NULL,0),(9,'routing-9','SystemVM Template (HyperV)','35d9360e-1951-4a94-8acc-f759e338aa1a',0,0,'SYSTEM',0,32,'http://download.cloud.com/templates/acton/acton-systemvm-02062012.vhd.bz2','VHD','2012-07-19 15:19:39',NULL,1,'f613f38c96bf039f2e5cbf92fa8ad4f8','SystemVM Template (HyperV)',0,0,15,1,0,1,0,'Hyperv',NULL,NULL,0),(200,'xs-tools.iso','xs-tools.iso','f722a941-0c0f-4a59-b060-41de29c4e4f7',1,1,'PERHOST',1,64,NULL,'ISO','2012-07-19 22:20:10',NULL,1,NULL,'xen-pv-drv-iso',0,0,1,0,0,0,1,'XenServer',NULL,NULL,0),(201,'vmware-tools.iso','vmware-tools.iso','b6ecb286-861a-4c76-9978-410ba21fbbdf',1,1,'PERHOST',1,64,NULL,'ISO','2012-07-19 22:20:10',NULL,1,NULL,'VMware Tools Installer ISO',0,0,1,0,0,0,1,'VMware',NULL,NULL,0); +INSERT INTO `vm_template` VALUES (1,'routing-1','SystemVM Template (XenServer)','ea041c86-a510-4a66-af6d-94ac2261c075',0,0,'SYSTEM',0,64,'http://download.cloud.com/templates/acton/acton-systemvm-02062012.vhd.bz2','VHD','2012-07-19 15:19:39',NULL,1,'f613f38c96bf039f2e5cbf92fa8ad4f8','SystemVM Template (XenServer)',0,0,15,1,0,1,0,'XenServer',NULL,NULL,0),(2,'centos53-x86_64','CentOS 5.3(64-bit) no GUI (XenServer)','77783498-a5f3-411f-be92-8965d06cc968',1,1,'BUILTIN',0,64,'http://download.cloud.com/templates/builtin/f59f18fb-ae94-4f97-afd2-f84755767aca.vhd.bz2','VHD','2012-07-19 15:19:39','2012-07-19 15:19:39',1,'b63d854a9560c013142567bbae8d98cf','CentOS 5.3(64-bit) no GUI (XenServer)',0,0,12,1,0,1,1,'XenServer',NULL,NULL,0),(3,'routing-3','SystemVM Template (KVM)','af37c452-9d00-4f48-80f1-1d4bd773c37e',0,0,'SYSTEM',0,64,'http://download.cloud.com/templates/acton/acton-systemvm-02062012.qcow2.bz2','QCOW2','2012-07-19 15:19:39',NULL,1,'2755de1f9ef2ce4d6f2bee2efbb4da92','SystemVM Template (KVM)',0,0,15,1,0,1,0,'KVM',NULL,NULL,0),(4,'centos55-x86_64','CentOS 5.5(64-bit) no GUI (KVM)','1f3f38ea-e0ee-4f21-92ab-27ca5949538e',1,1,'BUILTIN',0,64,'http://download.cloud.com/releases/2.2.0/eec2209b-9875-3c8d-92be-c001bd8a0faf.qcow2.bz2','QCOW2','2012-07-19 15:19:39',NULL,1,'ed0e788280ff2912ea40f7f91ca7a249','CentOS 5.5(64-bit) no GUI (KVM)',0,0,112,1,0,1,1,'KVM',NULL,NULL,0),(5,'centos56-x86_64-xen','CentOS 5.6(64-bit) no GUI (XenServer)','14a118d5-5803-48c1-82c9-85882d1ff97c',1,1,'BUILTIN',0,64,'http://download.cloud.com/templates/builtin/centos56-x86_64.vhd.bz2','VHD','2012-07-19 15:19:39',NULL,1,'905cec879afd9c9d22ecc8036131a180','CentOS 5.6(64-bit) no GUI (XenServer)',0,0,12,1,0,1,1,'XenServer',NULL,NULL,0),(7,'centos53-x64','CentOS 5.3(64-bit) no GUI (vSphere)','4801c38d-16f1-494c-ad95-2f7c89bac8e2',1,1,'BUILTIN',0,64,'http://download.cloud.com/releases/2.2.0/CentOS5.3-x86_64.ova','OVA','2012-07-19 15:19:39',NULL,1,'f6f881b7f2292948d8494db837fe0f47','CentOS 5.3(64-bit) no GUI (vSphere)',0,0,12,1,0,1,1,'VMware',NULL,NULL,0),(8,'routing-8','SystemVM Template (vSphere)','5a4a9ab4-7cdd-4d4a-b3ff-b79ae165df96',0,0,'SYSTEM',0,32,'http://download.cloud.com/templates/burbank/burbank-systemvm-08012012.ova','OVA','2012-07-19 15:19:39',NULL,1,'7137e453f950079ea2ba6feaafd939e8','SystemVM Template (vSphere)',0,0,15,1,0,1,0,'VMware',NULL,NULL,0),(9,'routing-9','SystemVM Template (HyperV)','35d9360e-1951-4a94-8acc-f759e338aa1a',0,0,'SYSTEM',0,32,'http://download.cloud.com/templates/acton/acton-systemvm-02062012.vhd.bz2','VHD','2012-07-19 15:19:39',NULL,1,'f613f38c96bf039f2e5cbf92fa8ad4f8','SystemVM Template (HyperV)',0,0,15,1,0,1,0,'Hyperv',NULL,NULL,0),(200,'xs-tools.iso','xs-tools.iso','f722a941-0c0f-4a59-b060-41de29c4e4f7',1,1,'PERHOST',1,64,NULL,'ISO','2012-07-19 22:20:10',NULL,1,NULL,'xen-pv-drv-iso',0,0,1,0,0,0,1,'XenServer',NULL,NULL,0),(201,'vmware-tools.iso','vmware-tools.iso','b6ecb286-861a-4c76-9978-410ba21fbbdf',1,1,'PERHOST',1,64,NULL,'ISO','2012-07-19 22:20:10',NULL,1,NULL,'VMware Tools Installer ISO',0,0,1,0,0,0,1,'VMware',NULL,NULL,0); /*!40000 ALTER TABLE `vm_template` ENABLE KEYS */; UNLOCK TABLES; diff --git a/test/integration/component/test_host_high_availability.py b/test/integration/component/test_host_high_availability.py new file mode 100644 index 00000000000..a019c0970f4 --- /dev/null +++ b/test/integration/component/test_host_high_availability.py @@ -0,0 +1,796 @@ +# -*- encoding: utf-8 -*- +# 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 + +""" P1 tests for dedicated Host high availability +""" +#Import Local Modules +import marvin +from marvin.cloudstackTestCase import * +from marvin.cloudstackAPI import * +from integration.lib.utils import * +from integration.lib.base import * +from integration.lib.common import * +from marvin import remoteSSHClient +import datetime + + +class Services: + """ Dedicated host HA test cases """ + + def __init__(self): + self.services = { + "account": { + "email": "test@test.com", + "firstname": "HA", + "lastname": "HA", + "username": "HA", + # Random characters are appended for unique + # username + "password": "password", + }, + "service_offering_with_ha": { + "name": "Tiny Instance With HA Enabled", + "displaytext": "Tiny Instance", + "cpunumber": 1, + "cpuspeed": 100, # in MHz + "memory": 64, # In MBs + }, + "service_offering_without_ha": { + "name": "Tiny Instance Without HA", + "displaytext": "Tiny Instance", + "cpunumber": 1, + "cpuspeed": 100, # in MHz + "memory": 64, # In MBs + }, + "virtual_machine": { + "displayname": "VM", + "username": "root", + "password": "password", + "ssh_port": 22, + "hypervisor": 'XenServer', + # Hypervisor type should be same as + # hypervisor type of cluster + "privateport": 22, + "publicport": 22, + "protocol": 'TCP', + }, + "ostypeid": '6a6b23aa-5e59-4b53-8161-0ad1aa5990f1', + "timeout": 100, + } + +class TestHostHighAvailability(cloudstackTestCase): + """ Dedicated host HA test cases """ + + @classmethod + def setUpClass(cls): + + cls.api_client = super( + TestHostHighAvailability, + cls + ).getClsTestClient().getApiClient() + cls.services = Services().services + # Get Zone, Domain and templates + cls.domain = get_domain( + cls.api_client, + cls.services + ) + cls.zone = get_zone( + cls.api_client, + cls.services + ) + + cls.template = get_template( + cls.api_client, + cls.zone.id, + cls.services["ostypeid"] + ) + cls.services["virtual_machine"]["zoneid"] = cls.zone.id + cls.services["virtual_machine"]["template"] = cls.template.id + + cls.service_offering_with_ha = ServiceOffering.create( + cls.api_client, + cls.services["service_offering_with_ha"], + offerha=True + ) + + cls.service_offering_without_ha = ServiceOffering.create( + cls.api_client, + cls.services["service_offering_without_ha"], + offerha=False + ) + + cls._cleanup = [ + cls.service_offering_with_ha, + cls.service_offering_without_ha, + ] + return + + @classmethod + def tearDownClass(cls): + try: + #Cleanup resources used + cleanup_resources(cls.api_client, cls._cleanup) + except Exception as e: + raise Exception("Warning: Exception during cleanup : %s" % e) + return + + def setUp(self): + self.apiclient = self.testClient.getApiClient() + self.dbclient = self.testClient.getDbConnection() + self.account = Account.create( + self.apiclient, + self.services["account"], + admin=True, + domainid=self.domain.id + ) + self.cleanup = [self.account] + return + + def tearDown(self): + try: + #Clean up, terminate the created accounts, domains etc + cleanup_resources(self.apiclient, self.cleanup) + self.testClient.close() + except Exception as e: + raise Exception("Warning: Exception during cleanup : %s" % e) + return + + def test_01_vm_deployment_with_compute_offering_with_ha_enabled(self): + """ Test VM deployments (Create HA enabled Compute Service Offering and VM) """ + + # Steps, + #1. Create a Compute service offering with the “Offer HA” option selected. + #2. Create a Guest VM with the compute service offering created above. + # Validations, + #1. Ensure that the offering is created and that in the UI the “Offer HA” field is enabled (Yes) + #The listServiceOffering API should list “offerha” as true. + #2. Select the newly created VM and ensure that the Compute offering field value lists the compute service offering that was selected. + # Also, check that the HA Enabled field is enabled “Yes”. + + #list and validate above created service offering with Ha enabled + list_service_response = list_service_offering( + self.apiclient, + id=self.service_offering_with_ha.id + ) + self.assertEqual( + isinstance(list_service_response, list), + True, + "listServiceOfferings returned invalid object in response." + ) + self.assertNotEqual( + len(list_service_response), + 0, + "listServiceOfferings returned empty list." + ) + self.assertEqual( + list_service_response[0].offerha, + True, + "The service offering is not HA enabled" + ) + + #create virtual machine with the service offering with Ha enabled + virtual_machine = VirtualMachine.create( + self.apiclient, + self.services["virtual_machine"], + accountid=self.account.account.name, + domainid=self.account.account.domainid, + serviceofferingid=self.service_offering_with_ha.id + ) + vms = VirtualMachine.list( + self.apiclient, + id=virtual_machine.id, + listall=True + ) + self.assertEqual( + isinstance(vms, list), + True, + "listVirtualMachines returned invalid object in response." + ) + self.assertNotEqual( + len(vms), + 0, + "listVirtualMachines returned empty list." + ) + self.debug("Deployed VM on host: %s" % vms[0].hostid) + self.assertEqual( + vms[0].haenable, + True, + "VM not created with HA enable tag" + ) + + def test_02_no_vm_creation_on_host_with_haenabled(self): + """ Verify you can not create new VMs on hosts with an ha.tag """ + + # Steps, + #1. Fresh install CS (Bonita) that supports this feature + #2. Create Basic zone, pod, cluster, add 3 hosts to cluster (host1, host2, host3), secondary & primary Storage + #3. When adding host3, assign the HA host tag. + #4. You should already have a compute service offering with HA already create from above. If not, create one for HA. + #5. Create VMs with the service offering with and without the HA tag + # Validations, + #Check to make sure the newly created VM is not on any HA enabled hosts + #The VM should be created only on host1 or host2 and never host3 (HA enabled) + + #create and verify virtual machine with HA enabled service offering + virtual_machine_with_ha = VirtualMachine.create( + self.apiclient, + self.services["virtual_machine"], + accountid=self.account.account.name, + domainid=self.account.account.domainid, + serviceofferingid=self.service_offering_with_ha.id + ) + + vms = VirtualMachine.list( + self.apiclient, + id=virtual_machine_with_ha.id, + listall=True + ) + + self.assertEqual( + isinstance(vms, list), + True, + "listVirtualMachines returned invalid object in response." + ) + + self.assertNotEqual( + len(vms), + 0, + "listVirtualMachines returned empty list." + ) + + vm = vms[0] + + self.debug("Deployed VM on host: %s" % vm.hostid) + + #validate the virtual machine created is host Ha enabled + list_hosts_response = list_hosts( + self.apiclient, + id=vm.hostid + ) + self.assertEqual( + isinstance(list_hosts_response, list), + True, + "listHosts returned invalid object in response." + ) + + self.assertNotEqual( + len(list_hosts_response), + 0, + "listHosts retuned empty list in response." + ) + + self.assertEqual( + list_hosts_response[0].hahost, + False, + "VM created on HA enabled host." + ) + + #create and verify virtual machine with Ha disabled service offering + virtual_machine_without_ha = VirtualMachine.create( + self.apiclient, + self.services["virtual_machine"], + accountid=self.account.account.name, + domainid=self.account.account.domainid, + serviceofferingid=self.service_offering_without_ha.id + ) + + vms = VirtualMachine.list( + self.apiclient, + id=virtual_machine_without_ha.id, + listall=True + ) + + self.assertEqual( + isinstance(vms, list), + True, + "listVirtualMachines returned invalid object in response." + ) + + self.assertNotEqual( + len(vms), + 0, + "listVirtualMachines returned empty list." + ) + + vm = vms[0] + + self.debug("Deployed VM on host: %s" % vm.hostid) + + #verify that the virtual machine created on the host is Ha disabled + list_hosts_response = list_hosts( + self.apiclient, + id=vm.hostid + ) + self.assertEqual( + isinstance(list_hosts_response, list), + True, + "listHosts returned invalid object in response." + ) + + self.assertNotEqual( + len(list_hosts_response), + 0, + "listHosts returned empty list." + ) + + host = list_hosts_response[0] + + self.assertEqual( + host.hahost, + False, + "VM migrated to HA enabled host." + ) + + def test_03_cant_migrate_vm_to_host_with_ha_positive(self): + """ Verify you can not migrate VMs to hosts with an ha.tag (positive) """ + + # Steps, + #1. Create a Compute service offering with the “Offer HA” option selected. + #2. Create a Guest VM with the compute service offering created above. + #3. Select the VM and migrate VM to another host. Choose a “Suitable” host (i.e. host2) + # Validations + #The option from the “Migrate instance to another host” dialog box” should list host3 as “Not Suitable” for migration. + #Confirm that the VM is migrated to the “Suitable” host you selected (i.e. host2) + + #create and verify the virtual machine with HA enabled service offering + virtual_machine_with_ha = VirtualMachine.create( + self.apiclient, + self.services["virtual_machine"], + accountid=self.account.account.name, + domainid=self.account.account.domainid, + serviceofferingid=self.service_offering_with_ha.id + ) + + vms = VirtualMachine.list( + self.apiclient, + id=virtual_machine_with_ha.id, + listall=True, + ) + + self.assertEqual( + isinstance(vms, list), + True, + "List VMs should return valid response for deployed VM" + ) + + self.assertNotEqual( + len(vms), + 0, + "List VMs should return valid response for deployed VM" + ) + + vm = vms[0] + + self.debug("Deployed VM on host: %s" % vm.hostid) + + #Find out a Suitable host for VM migration + list_hosts_response = list_hosts( + self.apiclient, + ) + self.assertEqual( + isinstance(list_hosts_response, list), + True, + "The listHosts API returned the invalid list" + ) + + self.assertNotEqual( + len(list_hosts_response), + 0, + "The listHosts returned nothing." + ) + suitableHost = None + for host in list_hosts_response: + if host.suitableformigration == True and host.hostid != vm.hostid: + suitableHost = host + break + + self.assertTrue(suitableHost is not None, "suitablehost should not be None") + + #Migration of the VM to a suitable host + self.debug("Migrating VM-ID: %s to Host: %s" % (self.vm.id, suitableHost.id)) + + cmd = migrateVirtualMachine.migrateVirtualMachineCmd() + cmd.hostid = suitableHost.id + cmd.virtualmachineid = self.vm.id + self.apiclient.migrateVirtualMachine(cmd) + + #Verify that the VM migrated to a targeted Suitable host + list_vm_response = list_virtual_machines( + self.apiclient, + id=vm.id + ) + self.assertEqual( + isinstance(list_vm_response, list), + True, + "The listVirtualMachines returned the invalid list." + ) + + self.assertNotEqual( + list_vm_response, + None, + "The listVirtualMachines API returned nothing." + ) + + vm_response = list_vm_response[0] + + self.assertEqual( + vm_response.id, + vm.id, + "The virtual machine id and the the virtual machine from listVirtualMachines is not matching." + ) + + self.assertEqual( + vm_response.hostid, + suitableHost.id, + "The VM is not migrated to targeted suitable host." + ) + + def test_04_cant_migrate_vm_to_host_with_ha_negative(self): + """ Verify you can not migrate VMs to hosts with an ha.tag (negative) """ + + # Steps, + #1. Create a Compute service offering with the “Offer HA” option selected. + #2. Create a Guest VM with the compute service offering created above. + #3. Select the VM and migrate VM to another host. Choose a “Not Suitable” host. + # Validations, + #The option from the “Migrate instance to another host” dialog box” should list host3 as “Not Suitable” for migration. + #By design, The Guest VM can STILL can be migrated to host3 if the admin chooses to do so. + + #create and verify virtual machine with HA enabled service offering + virtual_machine_with_ha = VirtualMachine.create( + self.apiclient, + self.services["virtual_machine"], + accountid=self.account.account.name, + domainid=self.account.account.domainid, + serviceofferingid=self.service_offering_with_ha.id + ) + + vms = VirtualMachine.list( + self.apiclient, + id=virtual_machine_with_ha.id, + listall=True + ) + + self.assertEqual( + isinstance(vms, list), + True, + "The listVirtualMachines returned invalid object in response." + ) + + self.assertNotEqual( + len(vms), + 0, + "The listVirtualMachines returned empty response." + ) + + vm = vms[0] + + self.debug("Deployed VM on host: %s" % vm.hostid) + + #Find out Non-Suitable host for VM migration + list_hosts_response = list_hosts( + self.apiclient, + ) + self.assertEqual( + isinstance(list_hosts_response, list), + True, + "listHosts returned invalid object in response." + ) + + self.assertNotEqual( + len(list_hosts_response), + 0, + "listHosts returned empty response." + ) + + notSuitableHost = None + for host in list_hosts_response: + if not host.suitableformigration and host.hostid != vm.hostid: + notSuitableHost = host + break + + self.assertTrue(notSuitableHost is not None, "notsuitablehost should not be None") + + #Migrate VM to Non-Suitable host + self.debug("Migrating VM-ID: %s to Host: %s" % (vm.id, notSuitableHost.id)) + + cmd = migrateVirtualMachine.migrateVirtualMachineCmd() + cmd.hostid = notSuitableHost.id + cmd.virtualmachineid = vm.id + self.apiclient.migrateVirtualMachine(cmd) + + #Verify that the virtual machine got migrated to targeted Non-Suitable host + list_vm_response = list_virtual_machines( + self.apiclient, + id=vm.id + ) + self.assertEqual( + isinstance(list_vm_response, list), + True, + "listVirtualMachine returned invalid object in response." + ) + + self.assertNotEqual( + len(list_vm_response), + 0, + "listVirtualMachines returned empty response." + ) + + self.assertEqual( + list_vm_response[0].id, + vm.id, + "Virtual machine id with the virtual machine from listVirtualMachine is not matching." + ) + + self.assertEqual( + list_vm_response[0].hostid, + notSuitableHost.id, + "The detination host id of migrated VM is not matching." + ) + + def test_05_no_vm_with_ha_gets_migrated_to_ha_host_in_live_migration(self): + """ Verify that none of the VMs with HA enabled migrate to an ha tagged host during live migration """ + + # Steps, + #1. Fresh install CS (Bonita) that supports this feature + #2. Create Basic zone, pod, cluster, add 3 hosts to cluster (host1, host2, host3), secondary & primary Storage + #3. When adding host3, assign the HA host tag. + #4. Create VMs with and without the Compute Service Offering with the HA tag. + #5. Note the VMs on host1 and whether any of the VMs have their “HA enabled” flags enabled. + #6. Put host1 into maintenance mode. + # Validations, + #1. Make sure the VMs are created on either host1 or host2 and not on host3 + #2. Putting host1 into maintenance mode should trigger a live migration. Make sure the VMs are not migrated to HA enabled host3. + + # create and verify virtual machine with HA disabled service offering + virtual_machine_with_ha = VirtualMachine.create( + self.apiclient, + self.services["virtual_machine"], + accountid=self.account.account.name, + domainid=self.account.account.domainid, + serviceofferingid=self.service_offering_with_ha.id + ) + + vms = VirtualMachine.list( + self.apiclient, + id=virtual_machine_with_ha.id, + listall=True + ) + + self.assertEqual( + isinstance(vms, list), + True, + "List VMs should return valid response for deployed VM" + ) + + self.assertNotEqual( + len(vms), + 0, + "List VMs should return valid response for deployed VM" + ) + + vm_with_ha_enabled = vms[0] + + #Verify the virtual machine got created on non HA host + list_hosts_response = list_hosts( + self.apiclient, + id=vm_with_ha_enabled.hostid + ) + self.assertEqual( + isinstance(list_hosts_response, list), + True, + "Check list response returns a valid list" + ) + + self.assertNotEqual( + len(list_hosts_response), + 0, + "Check Host is available" + ) + + self.assertEqual( + list_hosts_response[0].hahost, + False, + "The virtual machine is not ha enabled so check if VM is created on host which is also not ha enabled" + ) + + #put the Host in maintainance mode + self.debug("Enabling maintenance mode for host %s" % vm_with_ha_enabled.hostid) + cmd = prepareHostForMaintenance.prepareHostForMaintenanceCmd() + cmd.id = vm_with_ha_enabled.hostid + self.apiclient.prepareHostForMaintenance(cmd) + + timeout = self.services["timeout"] + + #verify the VM live migration happened to another running host + self.debug("Waiting for VM to come up") + wait_for_vm( + self.apiclient, + virtualmachineid=vm_with_ha_enabled.id, + interval=timeout + ) + + vms = VirtualMachine.list( + self.apiclient, + id=vm_with_ha_enabled.id, + listall=True, + ) + + self.assertEqual( + isinstance(vms, list), + True, + "List VMs should return valid response for deployed VM" + ) + + self.assertNotEqual( + len(vms), + 0, + "List VMs should return valid response for deployed VM" + ) + + vm_with_ha_enabled1 = vms[0] + + list_hosts_response = list_hosts( + self.apiclient, + id=vm_with_ha_enabled1.hostid + ) + self.assertEqual( + isinstance(list_hosts_response, list), + True, + "Check list response returns a valid list" + ) + + self.assertNotEqual( + len(list_hosts_response), + 0, + "Check Host is available" + ) + + self.assertEqual( + list_hosts_response[0].hahost, + False, + "The virtual machine is not ha enabled so check if VM is created on host which is also not ha enabled" + ) + + self.debug("Disabling the maintenance mode for host %s" % vm_with_ha_enabled.hostid) + cmd = cancelHostMaintenance.cancelHostMaintenanceCmd() + cmd.id = vm_with_ha_enabled.hostid + self.apiclient.cancelHostMaintenance(cmd) + + def test_06_no_vm_without_ha_gets_migrated_to_ha_host_in_live_migration(self): + """ Verify that none of the VMs without HA enabled migrate to an ha tagged host during live migration """ + + # Steps, + #1. Fresh install CS (Bonita) that supports this feature + #2. Create Basic zone, pod, cluster, add 3 hosts to cluster (host1, host2, host3), secondary & primary Storage + #3. When adding host3, assign the HA host tag. + #4. Create VMs with and without the Compute Service Offering with the HA tag. + #5. Note the VMs on host1 and whether any of the VMs have their “HA enabled” flags enabled. + #6. Put host1 into maintenance mode. + # Validations, + #1. Make sure the VMs are created on either host1 or host2 and not on host3 + #2. Putting host1 into maintenance mode should trigger a live migration. Make sure the VMs are not migrated to HA enabled host3. + + # create and verify virtual machine with HA disabled service offering + virtual_machine_without_ha = VirtualMachine.create( + self.apiclient, + self.services["virtual_machine"], + accountid=self.account.account.name, + domainid=self.account.account.domainid, + serviceofferingid=self.service_offering_without_ha.id + ) + + vms = VirtualMachine.list( + self.apiclient, + id=virtual_machine_without_ha.id, + listall=True + ) + + self.assertEqual( + isinstance(vms, list), + True, + "List VMs should return valid response for deployed VM" + ) + + self.assertNotEqual( + len(vms), + 0, + "List VMs should return valid response for deployed VM" + ) + + vm_with_ha_disabled = vms[0] + + #Verify the virtual machine got created on non HA host + list_hosts_response = list_hosts( + self.apiclient, + id=vm_with_ha_disabled.hostid + ) + self.assertEqual( + isinstance(list_hosts_response, list), + True, + "Check list response returns a valid list" + ) + + self.assertNotEqual( + len(list_hosts_response), + 0, + "Check Host is available" + ) + + self.assertEqual( + list_hosts_response[0].hahost, + False, + "The virtual machine is not ha enabled so check if VM is created on host which is also not ha enabled" + ) + + #put the Host in maintainance mode + self.debug("Enabling maintenance mode for host %s" % vm_with_ha_disabled.hostid) + cmd = prepareHostForMaintenance.prepareHostForMaintenanceCmd() + cmd.id = vm_with_ha_disabled.hostid + self.apiclient.prepareHostForMaintenance(cmd) + + timeout = self.services["timeout"] + + #verify the VM live migration happened to another running host + self.debug("Waiting for VM to come up") + wait_for_vm( + self.apiclient, + virtualmachineid=vm_with_ha_disabled.id, + interval=timeout + ) + + vms = VirtualMachine.list( + self.apiclient, + id=vm_with_ha_disabled.id, + listall=True + ) + + self.assertEqual( + isinstance(vms, list), + True, + "List VMs should return valid response for deployed VM" + ) + + self.assertNotEqual( + len(vms), + 0, + "List VMs should return valid response for deployed VM" + ) + + list_hosts_response = list_hosts( + self.apiclient, + id=vms[0].hostid + ) + self.assertEqual( + isinstance(list_hosts_response, list), + True, + "Check list response returns a valid list" + ) + + self.assertNotEqual( + len(list_hosts_response), + 0, + "Check Host is available" + ) + + self.assertEqual( + list_hosts_response[0].hahost, + False, + "The virtual machine is not ha enabled so check if VM is created on host which is also not ha enabled" + ) + + self.debug("Disabling the maintenance mode for host %s" % vm_with_ha_disabled.hostid) + cmd = cancelHostMaintenance.cancelHostMaintenanceCmd() + cmd.id = vm_with_ha_disabled.hostid + self.apiclient.cancelHostMaintenance(cmd) diff --git a/test/integration/lib/common.py b/test/integration/lib/common.py index 5a644aaf16a..630971c606f 100644 --- a/test/integration/lib/common.py +++ b/test/integration/lib/common.py @@ -185,6 +185,27 @@ def wait_for_ssvms(apiclient, zoneid, podid, interval=60): break return +def wait_for_vm(apiclient, virtualmachineid, interval=60): + """After setup wait for VM to come Up""" + + time.sleep(interval) + timeout = 40 + while True: + list_vm_response = list_virtual_machines( + apiclient, + id=virtualmachineid + ) + vm = list_vm_response[0] + if vm.state != 'Running': + # Sleep to ensure VM is Up and Running + time.sleep(interval) + timeout = timeout - 1 + elif vm.state == 'Running': + break + elif timeout == 0: + raise Exception("VM failed to come up") + break + return def download_builtin_templates(apiclient, zoneid, hypervisor, host, linklocalip, interval=60): diff --git a/ui/css/cloudstack3.css b/ui/css/cloudstack3.css index c7e84c78798..b8fc215d3f7 100644 --- a/ui/css/cloudstack3.css +++ b/ui/css/cloudstack3.css @@ -9156,6 +9156,19 @@ div.panel.ui-dialog div.list-view div.fixed-header { /** Dialog tagger*/ .ui-dialog .tagger { + width: 375px; +} + +.ui-dialog .tagger .tag-info { + display: none; +} + +.ui-dialog.editTags .ui-button { + float: right; +} + +.ui-dialog.editTags .ui-dialog-buttonpane { + float: right; } .ui-dialog .tagger .field { @@ -9634,21 +9647,43 @@ div.panel.ui-dialog div.list-view div.fixed-header { float: left; } -/*Configure ACL dialog*/ -.ui-dialog.configure-acl { +/*Configure ACL dialog / VM tier list view dialog*/ +.ui-dialog.configure-acl .multi-edit { + width: 866px; +} + +.ui-dialog.configure-acl .multi-edit table { + max-width: none; } .ui-dialog.configure-acl .ui-dialog-buttonpane { - /*+placement:shift 709px -2px;*/ + /*+placement:shift 722px -2px;*/ position: relative; - left: 709px; + left: 722px; top: -2px; } +.ui-dialog.configure-acl div.view.list-view { + max-height: 474px; +} + .ui-dialog.configure-acl .multi-edit .data { - width: 807px; + width: 901px; + padding: 0; + margin: 0; height: 370px; overflow: auto; + overflow-x: hidden; +} + +.ui-dialog.configure-acl .multi-edit .data .multi-actions { + min-width: none !important; + max-width: none !important; +} + +.ui-dialog.configure-acl .view.list-view table.body tr td.actions { + width: 184px !important; + max-width: 184px !important; } /*Autoscaler*/ @@ -9657,6 +9692,19 @@ div.panel.ui-dialog div.list-view div.fixed-header { max-height: 600px; } +.ui-dialog div.autoscaler .detail-actions { +} + +.ui-dialog div.autoscaler .detail-actions .buttons { + float: right; + margin-right: 6px; +} + +.ui-dialog div.autoscaler .detail-actions .buttons .action { + width: 32px; + float: left; +} + .ui-dialog div.autoscaler div.form-container div.form-item[rel=securityGroups] { display: block; width: 370px; @@ -10244,3 +10292,11 @@ div.ui-dialog div.autoscaler div.field-group div.form-container form div.form-it background-position: -230px -615px; } +.editTags .icon { + background-position: -228px -65px; +} + +.editTags:hover .icon { + background-position: -228px -646px; +} + diff --git a/ui/images/sprites.png b/ui/images/sprites.png index f1f5b8c5820..db95cca7b6e 100644 Binary files a/ui/images/sprites.png and b/ui/images/sprites.png differ diff --git a/ui/index.jsp b/ui/index.jsp index 71f869d8cd6..a83467bfdb2 100644 --- a/ui/index.jsp +++ b/ui/index.jsp @@ -5,7 +5,6 @@ <% long now = System.currentTimeMillis(); %> - @@ -17,7 +16,6 @@ - @@ -25,7 +23,6 @@
-
@@ -38,22 +35,18 @@
-
-
- " /> -
-

@@ -113,7 +104,6 @@
-
@@ -284,7 +274,6 @@
-
@@ -296,7 +285,6 @@
-
@@ -313,7 +301,6 @@
-
@@ -323,7 +310,6 @@
-
@@ -336,7 +322,6 @@
-
@@ -349,7 +334,6 @@
-
@@ -362,7 +346,6 @@
-
@@ -375,7 +358,6 @@
-
@@ -388,7 +370,6 @@
-
@@ -406,7 +387,6 @@
-
@@ -419,7 +399,6 @@
-
@@ -427,7 +406,6 @@
-
@@ -473,7 +451,6 @@
-
@@ -484,7 +461,6 @@
-
 
-
@@ -578,13 +553,11 @@
-
-
  • -
    -
    @@ -614,14 +585,12 @@
  • -
    -
    @@ -632,7 +601,6 @@
  • -
    @@ -640,7 +608,6 @@
    -
  • -
    @@ -662,7 +628,6 @@
    -
    @@ -673,13 +638,11 @@
  • -
    -
    @@ -697,7 +660,6 @@
    -
    @@ -714,7 +676,6 @@
    -
    @@ -731,7 +692,6 @@
    -
    @@ -748,7 +708,6 @@
    -
    @@ -762,7 +721,6 @@
    -
    @@ -770,7 +728,6 @@
    -
      @@ -788,7 +745,6 @@
    -
      @@ -803,7 +759,6 @@
    -
    @@ -871,7 +826,6 @@
    -
    @@ -915,7 +869,6 @@
    -
    @@ -934,7 +887,6 @@
    -
    @@ -1598,8 +1550,6 @@ - - @@ -1656,6 +1606,8 @@