Search in sources :

Example 1 with DataCenterDao

use of com.cloud.dc.dao.DataCenterDao in project cloudstack by apache.

the class DhcpSubNetRules method accept.

@Override
public boolean accept(final NetworkTopologyVisitor visitor, final VirtualRouter router) throws ResourceUnavailableException {
    _router = router;
    UserVmDao userVmDao = visitor.getVirtualNetworkApplianceFactory().getUserVmDao();
    final UserVmVO vm = userVmDao.findById(_profile.getId());
    userVmDao.loadDetails(vm);
    NicDao nicDao = visitor.getVirtualNetworkApplianceFactory().getNicDao();
    // check if this is not the primary subnet.
    final NicVO domrGuestNic = nicDao.findByInstanceIdAndIpAddressAndVmtype(_router.getId(), nicDao.getIpAddress(_nic.getNetworkId(), _router.getId()), VirtualMachine.Type.DomainRouter);
    // networks.
    if (!NetUtils.sameSubnet(domrGuestNic.getIPv4Address(), _nic.getIPv4Address(), _nic.getIPv4Netmask())) {
        final NicIpAliasDao nicIpAliasDao = visitor.getVirtualNetworkApplianceFactory().getNicIpAliasDao();
        final List<NicIpAliasVO> aliasIps = nicIpAliasDao.listByNetworkIdAndState(domrGuestNic.getNetworkId(), NicIpAlias.State.active);
        boolean ipInVmsubnet = false;
        for (final NicIpAliasVO alias : aliasIps) {
            // check if any of the alias ips belongs to the Vm's subnet.
            if (NetUtils.sameSubnet(alias.getIp4Address(), _nic.getIPv4Address(), _nic.getIPv4Netmask())) {
                ipInVmsubnet = true;
                break;
            }
        }
        PublicIp routerPublicIP = null;
        DataCenterDao dcDao = visitor.getVirtualNetworkApplianceFactory().getDcDao();
        final DataCenter dc = dcDao.findById(_router.getDataCenterId());
        if (ipInVmsubnet == false) {
            try {
                if (_network.getTrafficType() == TrafficType.Guest && _network.getGuestType() == GuestType.Shared) {
                    HostPodDao podDao = visitor.getVirtualNetworkApplianceFactory().getPodDao();
                    podDao.findById(vm.getPodIdToDeployIn());
                    final Account caller = CallContext.current().getCallingAccount();
                    VlanDao vlanDao = visitor.getVirtualNetworkApplianceFactory().getVlanDao();
                    final List<VlanVO> vlanList = vlanDao.listVlansByNetworkIdAndGateway(_network.getId(), _nic.getIPv4Gateway());
                    final List<Long> vlanDbIdList = new ArrayList<Long>();
                    for (final VlanVO vlan : vlanList) {
                        vlanDbIdList.add(vlan.getId());
                    }
                    IpAddressManager ipAddrMgr = visitor.getVirtualNetworkApplianceFactory().getIpAddrMgr();
                    if (dc.getNetworkType() == NetworkType.Basic) {
                        routerPublicIP = ipAddrMgr.assignPublicIpAddressFromVlans(_router.getDataCenterId(), vm.getPodIdToDeployIn(), caller, Vlan.VlanType.DirectAttached, vlanDbIdList, _nic.getNetworkId(), null, _nic.getIPv4Gateway(), false);
                    } else {
                        routerPublicIP = ipAddrMgr.assignPublicIpAddressFromVlans(_router.getDataCenterId(), null, caller, Vlan.VlanType.DirectAttached, vlanDbIdList, _nic.getNetworkId(), null, _nic.getIPv4Gateway(), false);
                    }
                    _routerAliasIp = routerPublicIP.getAddress().addr();
                }
            } catch (final InsufficientAddressCapacityException e) {
                s_logger.info(e.getMessage());
                s_logger.info("unable to configure dhcp for this VM.");
                return false;
            }
            // this means we did not create an IP alias on the router.
            _nicAlias = new NicIpAliasVO(domrGuestNic.getId(), _routerAliasIp, _router.getId(), CallContext.current().getCallingAccountId(), _network.getDomainId(), _nic.getNetworkId(), _nic.getIPv4Gateway(), _nic.getIPv4Netmask());
            _nicAlias.setAliasCount(routerPublicIP.getIpMacAddress());
            nicIpAliasDao.persist(_nicAlias);
            final boolean result = visitor.visit(this);
            if (result == false) {
                final NicIpAliasVO ipAliasVO = nicIpAliasDao.findByInstanceIdAndNetworkId(_network.getId(), _router.getId());
                final PublicIp routerPublicIPFinal = routerPublicIP;
                Transaction.execute(new TransactionCallbackNoReturn() {

                    @Override
                    public void doInTransactionWithoutResult(final TransactionStatus status) {
                        nicIpAliasDao.expunge(ipAliasVO.getId());
                        IPAddressDao ipAddressDao = visitor.getVirtualNetworkApplianceFactory().getIpAddressDao();
                        ipAddressDao.unassignIpAddress(routerPublicIPFinal.getId());
                    }
                });
                throw new CloudRuntimeException("failed to configure ip alias on the router as a part of dhcp config");
            }
        }
        return true;
    }
    return true;
}
Also used : Account(com.cloud.user.Account) UserVmVO(com.cloud.vm.UserVmVO) NicDao(com.cloud.vm.dao.NicDao) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) ArrayList(java.util.ArrayList) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) NicIpAliasVO(com.cloud.vm.dao.NicIpAliasVO) NicIpAliasDao(com.cloud.vm.dao.NicIpAliasDao) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UserVmDao(com.cloud.vm.dao.UserVmDao) VlanVO(com.cloud.dc.VlanVO) NicVO(com.cloud.vm.NicVO) VlanDao(com.cloud.dc.dao.VlanDao) PublicIp(com.cloud.network.addr.PublicIp) IPAddressDao(com.cloud.network.dao.IPAddressDao) DataCenterDao(com.cloud.dc.dao.DataCenterDao) HostPodDao(com.cloud.dc.dao.HostPodDao) IpAddressManager(com.cloud.network.IpAddressManager) DataCenter(com.cloud.dc.DataCenter)

Example 2 with DataCenterDao

use of com.cloud.dc.dao.DataCenterDao 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();
    NicDao nicDao = visitor.getVirtualNetworkApplianceFactory().getNicDao();
    VpcVirtualNetworkApplianceService routerService = visitor.getVirtualNetworkApplianceFactory().getRouterService();
    // de-associate IPs before unplugging nics
    if (!nicsToUnplug.isEmpty()) {
        NetworkTopologyContext networkTopologyContext = visitor.getVirtualNetworkApplianceFactory().getNetworkTopologyContext();
        final DataCenterDao dcDao = visitor.getVirtualNetworkApplianceFactory().getDcDao();
        final DataCenterVO dcVO = dcDao.findById(router.getDataCenterId());
        final NetworkTopology networkTopology = networkTopologyContext.retrieveNetworkTopology(dcVO);
        final String typeString = "vpc ip association before unplugging nics";
        final boolean isPodLevelException = false;
        final boolean failWhenDisconnect = false;
        final Long podId = null;
        final VpcIpAssociationRules ipAssociationRules = new VpcIpAssociationRules(_network, _ipAddresses);
        final boolean result = networkTopology.applyRules(_network, router, typeString, isPodLevelException, podId, failWhenDisconnect, new RuleApplierWrapper<RuleApplier>(ipAssociationRules));
        if (!result) {
            s_logger.warn("Failed to de-associate IPs before unplugging nics");
            return false;
        }
    }
    // 1) Unplug the nics
    for (Entry<String, PublicIpAddress> entry : nicsToUnplug.entrySet()) {
        PublicIpAddress ip = entry.getValue();
        NicVO nic = nicDao.findByIp4AddressAndNetworkIdAndInstanceId(ip.getNetworkId(), _router.getId(), ip.getAddress().addr());
        if (nic != null) {
            s_logger.info("Collect network statistics for nic " + nic + " from router " + _router);
            routerService.collectNetworkStatistics(_router, nic);
        }
        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 : NicDao(com.cloud.vm.dao.NicDao) URI(java.net.URI) UserStatisticsDao(com.cloud.user.dao.UserStatisticsDao) PublicIpAddress(com.cloud.network.PublicIpAddress) VpcDao(com.cloud.network.vpc.dao.VpcDao) Network(com.cloud.network.Network) Commands(com.cloud.agent.manager.Commands) NetworkModel(com.cloud.network.NetworkModel) VpcVirtualNetworkApplianceService(com.cloud.network.VpcVirtualNetworkApplianceService) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) NicVO(com.cloud.vm.NicVO) DataCenterVO(com.cloud.dc.DataCenterVO) DataCenterDao(com.cloud.dc.dao.DataCenterDao) NetworkTopologyContext(org.apache.cloudstack.network.topology.NetworkTopologyContext) NetworkUsageCommand(com.cloud.agent.api.NetworkUsageCommand) NicProfile(com.cloud.vm.NicProfile) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) VpcVO(com.cloud.network.vpc.VpcVO) NetworkTopology(org.apache.cloudstack.network.topology.NetworkTopology) VirtualMachineManager(com.cloud.vm.VirtualMachineManager) HashMap(java.util.HashMap) Map(java.util.Map) UserStatisticsVO(com.cloud.user.UserStatisticsVO)

Aggregations

DataCenterDao (com.cloud.dc.dao.DataCenterDao)2 NicVO (com.cloud.vm.NicVO)2 NicDao (com.cloud.vm.dao.NicDao)2 NetworkUsageCommand (com.cloud.agent.api.NetworkUsageCommand)1 Commands (com.cloud.agent.manager.Commands)1 DataCenter (com.cloud.dc.DataCenter)1 DataCenterVO (com.cloud.dc.DataCenterVO)1 VlanVO (com.cloud.dc.VlanVO)1 HostPodDao (com.cloud.dc.dao.HostPodDao)1 VlanDao (com.cloud.dc.dao.VlanDao)1 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)1 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)1 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)1 IpAddressManager (com.cloud.network.IpAddressManager)1 Network (com.cloud.network.Network)1 NetworkModel (com.cloud.network.NetworkModel)1 PublicIpAddress (com.cloud.network.PublicIpAddress)1 VpcVirtualNetworkApplianceService (com.cloud.network.VpcVirtualNetworkApplianceService)1 PublicIp (com.cloud.network.addr.PublicIp)1 IPAddressDao (com.cloud.network.dao.IPAddressDao)1