use of com.cloud.vm.ReservationContext in project cloudstack by apache.
the class NuageVspElementTest method testShutdownVpc.
@Test
public void testShutdownVpc() throws Exception {
final Vpc vpc = mock(Vpc.class);
when(vpc.getUuid()).thenReturn("aaaaaa");
when(vpc.getState()).thenReturn(Vpc.State.Inactive);
when(vpc.getDomainId()).thenReturn(NETWORK_ID);
when(vpc.getZoneId()).thenReturn(NETWORK_ID);
when(vpc.getId()).thenReturn(NETWORK_ID);
final DomainVO dom = mock(DomainVO.class);
when(dom.getName()).thenReturn("domain");
when(_domainDao.findById(NETWORK_ID)).thenReturn(dom);
final Account acc = mock(Account.class);
when(acc.getAccountName()).thenReturn("accountname");
final ReservationContext context = mock(ReservationContext.class);
when(context.getDomain()).thenReturn(dom);
when(context.getAccount()).thenReturn(acc);
PhysicalNetworkVO physNet = mock(PhysicalNetworkVO.class);
when(physNet.getIsolationMethods()).thenReturn(Lists.newArrayList(PhysicalNetwork.IsolationMethod.VSP.name()));
when(physNet.getId()).thenReturn(NETWORK_ID);
when(_physicalNetworkDao.listByZone(NETWORK_ID)).thenReturn(Lists.newArrayList(physNet));
final HostVO host = mock(HostVO.class);
when(host.getId()).thenReturn(NETWORK_ID);
final NuageVspDeviceVO nuageVspDevice = mock(NuageVspDeviceVO.class);
when(nuageVspDevice.getHostId()).thenReturn(NETWORK_ID);
when(_nuageVspDao.listByPhysicalNetwork(NETWORK_ID)).thenReturn(Lists.newArrayList(nuageVspDevice));
when(_hostDao.findById(NETWORK_ID)).thenReturn(host);
when(_nuageVspManager.getNuageVspHost(NETWORK_ID)).thenReturn(host);
DomainRouterVO domainRouter = mock(DomainRouterVO.class);
when(domainRouter.getUuid()).thenReturn("aaaaaa");
when(_domainRouterDao.listByVpcId(NETWORK_ID)).thenReturn(Lists.newArrayList(domainRouter));
final Answer answer = mock(Answer.class);
when(answer.getResult()).thenReturn(true);
when(_agentManager.easySend(eq(NETWORK_ID), (Command) any())).thenReturn(answer);
assertTrue(_nuageVspElement.shutdownVpc(vpc, context));
}
use of com.cloud.vm.ReservationContext in project cosmic by MissionCriticalCloud.
the class VpcManagerImpl method shutdownVpc.
@Override
public boolean shutdownVpc(final long vpcId) throws ConcurrentOperationException, ResourceUnavailableException {
final CallContext ctx = CallContext.current();
final Account caller = ctx.getCallingAccount();
// check if vpc exists
final Vpc vpc = _vpcDao.findById(vpcId);
if (vpc == null) {
throw new InvalidParameterValueException("Unable to find vpc by id " + vpcId);
}
// permission check
_accountMgr.checkAccess(caller, null, false, vpc);
// shutdown provider
s_logger.debug("Shutting down vpc " + vpc);
// TODO - shutdown all vpc resources here (ACLs, gateways, etc)
boolean success = true;
final List<Provider> providersToImplement = getVpcProviders(vpc.getId());
final ReservationContext context = new ReservationContextImpl(null, null, _accountMgr.getActiveUser(ctx.getCallingUserId()), caller);
for (final VpcProvider element : getVpcElements()) {
if (providersToImplement.contains(element.getProvider())) {
if (element.shutdownVpc(vpc, context)) {
s_logger.debug("Vpc " + vpc + " has been shutdown succesfully");
} else {
s_logger.warn("Vpc " + vpc + " failed to shutdown");
success = false;
}
}
}
return success;
}
use of com.cloud.vm.ReservationContext in project cosmic by MissionCriticalCloud.
the class VpcManagerImpl method updateVpc.
@Override
@ActionEvent(eventType = EventTypes.EVENT_VPC_UPDATE, eventDescription = "updating vpc")
public Vpc updateVpc(final long vpcId, final String vpcName, final String displayText, final String customId, final Boolean displayVpc, final Long vpcOfferingId, final String sourceNatList, final String syslogServerList) {
CallContext.current().setEventDetails(" Id: " + vpcId);
final Account caller = CallContext.current().getCallingAccount();
// Verify input parameters
final VpcVO vpcToUpdate = _vpcDao.findById(vpcId);
if (vpcToUpdate == null) {
throw new InvalidParameterValueException("Unable to find vpc by id " + vpcId);
}
_accountMgr.checkAccess(caller, null, false, vpcToUpdate);
final VpcVO vpc = _vpcDao.createForUpdate(vpcId);
boolean restartWithCleanupRequired = false;
if (vpcName != null) {
vpc.setName(vpcName);
}
if (displayText != null) {
vpc.setDisplayText(displayText);
}
if (customId != null) {
vpc.setUuid(customId);
}
if (displayVpc != null) {
vpc.setDisplay(displayVpc);
}
if (syslogServerList != null) {
vpc.setSyslogServerList(syslogServerList);
}
if (vpcOfferingId != null) {
final VpcOfferingVO newVpcOffering = _vpcOffDao.findById(vpcOfferingId);
if (newVpcOffering == null) {
throw new InvalidParameterValueException("Unable to find vpc offering by id " + vpcOfferingId);
}
if (vpcOfferingId == vpcToUpdate.getVpcOfferingId()) {
throw new InvalidParameterValueException("The vpc already has the specified offering, so not upgrading. Use restart+cleanup to rebuild.");
}
// check if the new VPC offering matches the network offerings in use
checkVpcOfferingServicesWithCurrentNetworkOfferings(vpcOfferingId, vpcToUpdate);
vpc.setVpcOfferingId(vpcOfferingId);
vpc.setRedundant(newVpcOffering.getRedundantRouter());
restartWithCleanupRequired = true;
// disassociate the public IPs if not required anymore
if (!hasSourceNatService(vpc)) {
boolean success = true;
final List<IPAddressVO> ipsToRelease = _ipAddressDao.listByVpc(vpcId, null);
s_logger.debug("Releasing ips for vpc id=" + vpcId + " as a part of vpc cleanup");
for (final IPAddressVO ipToRelease : ipsToRelease) {
success = success && _ipAddrMgr.disassociatePublicIpAddress(ipToRelease.getId(), CallContext.current().getCallingUserId(), caller);
if (!success) {
s_logger.warn("Failed to cleanup ip " + ipToRelease + " as a part of vpc id=" + vpcId + " cleanup");
}
}
}
}
vpc.setRestartRequired(restartWithCleanupRequired);
if (sourceNatList != null && !sourceNatList.isEmpty() && vpcOfferingId != null && !hasSourceNatService(vpc)) {
throw new InvalidParameterValueException("Source NAT is not enabled on the VPC, so source NAT list is not allowed!");
}
if (sourceNatList != null) {
vpc.setSourceNatList(sourceNatList);
}
if (vpcOfferingId != null && !hasSourceNatService(vpc) && hasSourceNatService(vpcToUpdate)) {
s_logger.warn("SourceNat service not available on VPC " + vpc.getName() + " so setting SourceNatList to null!");
vpc.setSourceNatList(null);
}
// Save the new config
if (_vpcDao.update(vpcId, vpc)) {
s_logger.debug("Updated VPC id=" + vpcId);
} else {
return null;
}
final Account callerAccount = CallContext.current().getCallingAccount();
final User callerUser = _accountMgr.getActiveUser(CallContext.current().getCallingUserId());
final ReservationContext context = new ReservationContextImpl(null, null, callerUser, callerAccount);
if (!vpc.isRedundant()) {
final List<DomainRouterVO> routers = _routerDao.listByVpcId(vpc.getId());
for (final DomainRouterVO router : routers) {
// Delete any non-MASTER router since we are supposed to run a single setup according to the new VPC offering
if (router.getRedundantState() != VirtualRouter.RedundantState.MASTER) {
try {
s_logger.warn("Deleting router " + router.getInstanceName() + " as we don't need it any more");
_routerMgr.destroyRouter(router.getId(), context.getAccount(), context.getCaller().getId());
} catch (final ResourceUnavailableException ex) {
s_logger.warn("Exception: ", ex);
throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage());
}
}
}
return _vpcDao.findById(vpcId);
}
// Restart the VPC when required
if (restartWithCleanupRequired) {
s_logger.debug("Will now restart+cleanup VPC id=" + vpcId);
try {
final boolean result = restartVpc(vpcId, true);
if (!result) {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to restart VPC");
}
} catch (final ResourceUnavailableException ex) {
s_logger.warn("Exception: ", ex);
throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage());
} catch (final ConcurrentOperationException ex) {
s_logger.warn("Exception: ", ex);
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
} catch (final InsufficientCapacityException ex) {
s_logger.info(ex.toString());
throw new ServerApiException(ApiErrorCode.INSUFFICIENT_CAPACITY_ERROR, ex.getMessage());
}
} else {
// LOOP over all routers
final List<DomainRouterVO> routers = _routerDao.listByVpcId(vpc.getId());
if (routers != null && !routers.isEmpty()) {
s_logger.debug("Updating routers of VPC " + vpc + " as a part of VPC update process");
for (final DomainRouterVO router : routers) {
// Validate that the router is running
if (router.getState() == VirtualMachine.State.Running) {
if (!_routerMgr.updateVR(vpc, router)) {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update VPC config");
}
}
}
}
}
return _vpcDao.findById(vpcId);
}
use of com.cloud.vm.ReservationContext in project cosmic by MissionCriticalCloud.
the class NetworkServiceImpl method deleteNetworkServiceProvider.
@Override
@ActionEvent(eventType = EventTypes.EVENT_SERVICE_PROVIDER_DELETE, eventDescription = "Deleting physical network ServiceProvider", async = true)
public boolean deleteNetworkServiceProvider(final Long id) throws ConcurrentOperationException, ResourceUnavailableException {
final PhysicalNetworkServiceProviderVO provider = _pNSPDao.findById(id);
if (provider == null) {
throw new InvalidParameterValueException("Network Service Provider id=" + id + "doesn't exist in the system");
}
// check if there are networks using this provider
final List<NetworkVO> networks = _networksDao.listByPhysicalNetworkAndProvider(provider.getPhysicalNetworkId(), provider.getProviderName());
if (networks != null && !networks.isEmpty()) {
throw new CloudRuntimeException("Provider is not deletable because there are active networks using this provider, please upgrade these networks to new network offerings");
}
final User callerUser = _accountMgr.getActiveUser(CallContext.current().getCallingUserId());
final Account callerAccount = _accountMgr.getActiveAccountById(callerUser.getAccountId());
// shutdown the provider instances
final ReservationContext context = new ReservationContextImpl(null, null, callerUser, callerAccount);
if (s_logger.isDebugEnabled()) {
s_logger.debug("Shutting down the service provider id=" + id + " on physical network: " + provider.getPhysicalNetworkId());
}
final NetworkElement element = _networkModel.getElementImplementingProvider(provider.getProviderName());
if (element == null) {
throw new InvalidParameterValueException("Unable to find the Network Element implementing the Service Provider '" + provider.getProviderName() + "'");
}
if (element != null && element.shutdownProviderInstances(provider, context)) {
provider.setState(PhysicalNetworkServiceProvider.State.Shutdown);
}
return _pNSPDao.remove(id);
}
use of com.cloud.vm.ReservationContext in project cosmic by MissionCriticalCloud.
the class NetworkServiceImpl method deleteNetwork.
@Override
@ActionEvent(eventType = EventTypes.EVENT_NETWORK_DELETE, eventDescription = "deleting network", async = true)
public boolean deleteNetwork(final long networkId, final boolean forced) {
final Account caller = CallContext.current().getCallingAccount();
// Verify network id
final NetworkVO network = _networksDao.findById(networkId);
if (network == null) {
// see NetworkVO.java
final InvalidParameterValueException ex = new InvalidParameterValueException("unable to find network with specified id");
ex.addProxyObject(String.valueOf(networkId), "networkId");
throw ex;
}
// don't allow to delete system network
if (isNetworkSystem(network)) {
final InvalidParameterValueException ex = new InvalidParameterValueException("Network with specified id is system and can't be removed");
ex.addProxyObject(network.getUuid(), "networkId");
throw ex;
}
final Account owner = _accountMgr.getAccount(network.getAccountId());
// Only Admin can delete Shared networks
if (network.getGuestType() == GuestType.Shared && !_accountMgr.isAdmin(caller.getId())) {
throw new InvalidParameterValueException("Only Admins can delete network with guest type " + GuestType.Shared);
}
// Perform permission check
_accountMgr.checkAccess(caller, null, true, network);
if (forced && !_accountMgr.isRootAdmin(caller.getId())) {
throw new InvalidParameterValueException("Delete network with 'forced' option can only be called by root admins");
}
// VPC networks should be checked for static routes before deletion
if (network.getVpcId() != null) {
// don't allow to remove network tier when there are static routes pointing to an ipaddress in the tier CIDR.
final List<? extends StaticRoute> routes = _staticRouteDao.listByVpcIdAndNotRevoked(network.getVpcId());
for (final StaticRoute route : routes) {
if (NetUtils.isIpWithtInCidrRange(route.getGwIpAddress(), network.getCidr())) {
throw new CloudRuntimeException("Can't delete network " + network.getName() + " as it has static routes " + "applied pointing to the CIDR of the network (" + network.getCidr() + "). Example static route: " + route.getCidr() + " to " + route.getGwIpAddress() + ". Please remove all the routes pointing to the " + "network tier CIDR before attempting to delete it.");
}
}
}
final User callerUser = _accountMgr.getActiveUser(CallContext.current().getCallingUserId());
final ReservationContext context = new ReservationContextImpl(null, null, callerUser, owner);
return _networkMgr.destroyNetwork(networkId, context, forced);
}
Aggregations