Search in sources :

Example 1 with VirtualMachineManager

use of com.cloud.vm.VirtualMachineManager in project cloudstack by apache.

the class NicPlugInOutRules method accept.

@Override
public boolean accept(final NetworkTopologyVisitor visitor, final VirtualRouter router) throws ResourceUnavailableException {
    _router = router;
    Pair<Map<String, PublicIpAddress>, Map<String, PublicIpAddress>> nicsToChange = getNicsToChangeOnRouter(visitor);
    Map<String, PublicIpAddress> nicsToPlug = nicsToChange.first();
    Map<String, PublicIpAddress> nicsToUnplug = nicsToChange.second();
    NetworkModel networkModel = visitor.getVirtualNetworkApplianceFactory().getNetworkModel();
    VirtualMachineManager itMgr = visitor.getVirtualNetworkApplianceFactory().getItMgr();
    // 1) Unplug the nics
    for (Entry<String, PublicIpAddress> entry : nicsToUnplug.entrySet()) {
        Network publicNtwk = null;
        try {
            publicNtwk = networkModel.getNetwork(entry.getValue().getNetworkId());
            URI broadcastUri = BroadcastDomainType.Vlan.toUri(entry.getKey());
            itMgr.removeVmFromNetwork(_router, publicNtwk, broadcastUri);
        } catch (ConcurrentOperationException e) {
            s_logger.warn("Failed to remove router " + _router + " from vlan " + entry.getKey() + " in public network " + publicNtwk + " due to ", e);
            return false;
        }
    }
    _netUsageCommands = new Commands(Command.OnError.Continue);
    VpcDao vpcDao = visitor.getVirtualNetworkApplianceFactory().getVpcDao();
    VpcVO vpc = vpcDao.findById(_router.getVpcId());
    // 2) Plug the nics
    for (String vlanTag : nicsToPlug.keySet()) {
        PublicIpAddress ip = nicsToPlug.get(vlanTag);
        // have to plug the nic(s)
        NicProfile defaultNic = new NicProfile();
        if (ip.isSourceNat()) {
            defaultNic.setDefaultNic(true);
        }
        defaultNic.setIPv4Address(ip.getAddress().addr());
        defaultNic.setIPv4Gateway(ip.getGateway());
        defaultNic.setIPv4Netmask(ip.getNetmask());
        defaultNic.setMacAddress(ip.getMacAddress());
        defaultNic.setBroadcastType(BroadcastDomainType.Vlan);
        defaultNic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(ip.getVlanTag()));
        defaultNic.setIsolationUri(IsolationType.Vlan.toUri(ip.getVlanTag()));
        NicProfile publicNic = null;
        Network publicNtwk = null;
        try {
            publicNtwk = networkModel.getNetwork(ip.getNetworkId());
            publicNic = itMgr.addVmToNetwork(_router, publicNtwk, defaultNic);
        } catch (ConcurrentOperationException e) {
            s_logger.warn("Failed to add router " + _router + " to vlan " + vlanTag + " in public network " + publicNtwk + " due to ", e);
        } catch (InsufficientCapacityException e) {
            s_logger.warn("Failed to add router " + _router + " to vlan " + vlanTag + " in public network " + publicNtwk + " due to ", e);
        } finally {
            if (publicNic == null) {
                s_logger.warn("Failed to add router " + _router + " to vlan " + vlanTag + " in public network " + publicNtwk);
                return false;
            }
        }
        // Create network usage commands. Send commands to router after
        // IPAssoc
        NetworkUsageCommand netUsageCmd = new NetworkUsageCommand(_router.getPrivateIpAddress(), _router.getInstanceName(), true, defaultNic.getIPv4Address(), vpc.getCidr());
        _netUsageCommands.addCommand(netUsageCmd);
        UserStatisticsDao userStatsDao = visitor.getVirtualNetworkApplianceFactory().getUserStatsDao();
        UserStatisticsVO stats = userStatsDao.findBy(_router.getAccountId(), _router.getDataCenterId(), publicNtwk.getId(), publicNic.getIPv4Address(), _router.getId(), _router.getType().toString());
        if (stats == null) {
            stats = new UserStatisticsVO(_router.getAccountId(), _router.getDataCenterId(), publicNic.getIPv4Address(), _router.getId(), _router.getType().toString(), publicNtwk.getId());
            userStatsDao.persist(stats);
        }
    }
    // VpcIpAssociation is done.
    return true;
}
Also used : NetworkUsageCommand(com.cloud.agent.api.NetworkUsageCommand) NicProfile(com.cloud.vm.NicProfile) URI(java.net.URI) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) UserStatisticsDao(com.cloud.user.dao.UserStatisticsDao) PublicIpAddress(com.cloud.network.PublicIpAddress) VpcDao(com.cloud.network.vpc.dao.VpcDao) VpcVO(com.cloud.network.vpc.VpcVO) Network(com.cloud.network.Network) Commands(com.cloud.agent.manager.Commands) NetworkModel(com.cloud.network.NetworkModel) VirtualMachineManager(com.cloud.vm.VirtualMachineManager) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) HashMap(java.util.HashMap) Map(java.util.Map) UserStatisticsVO(com.cloud.user.UserStatisticsVO)

Example 2 with VirtualMachineManager

use of com.cloud.vm.VirtualMachineManager in project cloudstack by apache.

the class PrivateGatewayRules method accept.

@Override
public boolean accept(final NetworkTopologyVisitor visitor, final VirtualRouter router) throws ResourceUnavailableException {
    _router = router;
    boolean result = false;
    try {
        final NetworkModel networkModel = visitor.getVirtualNetworkApplianceFactory().getNetworkModel();
        _network = networkModel.getNetwork(_privateGateway.getNetworkId());
        final NicProfileHelper nicProfileHelper = visitor.getVirtualNetworkApplianceFactory().getNicProfileHelper();
        final NicProfile requested = nicProfileHelper.createPrivateNicProfileForGateway(_privateGateway, _router);
        final NetworkHelper networkHelper = visitor.getVirtualNetworkApplianceFactory().getNetworkHelper();
        if (!networkHelper.checkRouterVersion(_router)) {
            s_logger.warn("Router requires upgrade. Unable to send command to router: " + _router.getId());
            return false;
        }
        final VirtualMachineManager itMgr = visitor.getVirtualNetworkApplianceFactory().getItMgr();
        _nicProfile = itMgr.addVmToNetwork(_router, _network, requested);
        // setup source nat
        if (_nicProfile != null) {
            _isAddOperation = true;
            // result = setupVpcPrivateNetwork(router, true, guestNic);
            result = visitor.visit(this);
        }
    } catch (final Exception ex) {
        s_logger.warn("Failed to create private gateway " + _privateGateway + " on router " + _router + " due to ", ex);
    } finally {
        if (!result) {
            s_logger.debug("Failed to setup gateway " + _privateGateway + " on router " + _router + " with the source nat. Will now remove the gateway.");
            _isAddOperation = false;
            final boolean isRemoved = destroyPrivateGateway(visitor);
            if (isRemoved) {
                s_logger.debug("Removed the gateway " + _privateGateway + " from router " + _router + " as a part of cleanup");
            } else {
                s_logger.warn("Failed to remove the gateway " + _privateGateway + " from router " + _router + " as a part of cleanup");
            }
        }
    }
    return result;
}
Also used : NetworkModel(com.cloud.network.NetworkModel) VirtualMachineManager(com.cloud.vm.VirtualMachineManager) NicProfileHelper(com.cloud.network.router.NicProfileHelper) NicProfile(com.cloud.vm.NicProfile) NetworkHelper(com.cloud.network.router.NetworkHelper) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException)

Example 3 with VirtualMachineManager

use of com.cloud.vm.VirtualMachineManager in project cloudstack by apache.

the class PrivateGatewayRules method destroyPrivateGateway.

protected boolean destroyPrivateGateway(final NetworkTopologyVisitor visitor) throws ConcurrentOperationException, ResourceUnavailableException {
    final NetworkModel networkModel = visitor.getVirtualNetworkApplianceFactory().getNetworkModel();
    if (!networkModel.isVmPartOfNetwork(_router.getId(), _privateGateway.getNetworkId())) {
        s_logger.debug("Router doesn't have nic for gateway " + _privateGateway + " so no need to removed it");
        return true;
    }
    final Network privateNetwork = networkModel.getNetwork(_privateGateway.getNetworkId());
    s_logger.debug("Releasing private ip for gateway " + _privateGateway + " from " + _router);
    _nicProfile = networkModel.getNicProfile(_router, privateNetwork.getId(), null);
    boolean result = visitor.visit(this);
    if (!result) {
        s_logger.warn("Failed to release private ip for gateway " + _privateGateway + " on router " + _router);
        return false;
    }
    // revoke network acl on the private gateway.
    final NetworkACLManager networkACLMgr = visitor.getVirtualNetworkApplianceFactory().getNetworkACLMgr();
    if (!networkACLMgr.revokeACLItemsForPrivateGw(_privateGateway)) {
        s_logger.debug("Failed to delete network acl items on " + _privateGateway + " from router " + _router);
        return false;
    }
    s_logger.debug("Removing router " + _router + " from private network " + privateNetwork + " as a part of delete private gateway");
    final VirtualMachineManager itMgr = visitor.getVirtualNetworkApplianceFactory().getItMgr();
    result = result && itMgr.removeVmFromNetwork(_router, privateNetwork, null);
    s_logger.debug("Private gateawy " + _privateGateway + " is removed from router " + _router);
    return result;
}
Also used : Network(com.cloud.network.Network) NetworkModel(com.cloud.network.NetworkModel) VirtualMachineManager(com.cloud.vm.VirtualMachineManager) NetworkACLManager(com.cloud.network.vpc.NetworkACLManager)

Aggregations

NetworkModel (com.cloud.network.NetworkModel)3 VirtualMachineManager (com.cloud.vm.VirtualMachineManager)3 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)2 Network (com.cloud.network.Network)2 NicProfile (com.cloud.vm.NicProfile)2 NetworkUsageCommand (com.cloud.agent.api.NetworkUsageCommand)1 Commands (com.cloud.agent.manager.Commands)1 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)1 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)1 PublicIpAddress (com.cloud.network.PublicIpAddress)1 NetworkHelper (com.cloud.network.router.NetworkHelper)1 NicProfileHelper (com.cloud.network.router.NicProfileHelper)1 NetworkACLManager (com.cloud.network.vpc.NetworkACLManager)1 VpcVO (com.cloud.network.vpc.VpcVO)1 VpcDao (com.cloud.network.vpc.dao.VpcDao)1 UserStatisticsVO (com.cloud.user.UserStatisticsVO)1 UserStatisticsDao (com.cloud.user.dao.UserStatisticsDao)1 URI (java.net.URI)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1