Search in sources :

Example 6 with PublicIp

use of com.cloud.network.addr.PublicIp 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, false);
                    } else {
                        routerPublicIP = ipAddrMgr.assignPublicIpAddressFromVlans(_router.getDataCenterId(), null, caller, Vlan.VlanType.DirectAttached, vlanDbIdList, _nic.getNetworkId(), null, 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 7 with PublicIp

use of com.cloud.network.addr.PublicIp in project cloudstack by apache.

the class BaremetaNetworkGuru method getBaremetalIp.

private void getBaremetalIp(NicProfile nic, Pod pod, VirtualMachineProfile vm, Network network, String requiredIp) throws InsufficientAddressCapacityException, ConcurrentOperationException {
    DataCenter dc = _dcDao.findById(pod.getDataCenterId());
    if (nic.getIPv4Address() == null) {
        s_logger.debug(String.format("Requiring ip address: %s", nic.getIPv4Address()));
        PublicIp ip = _ipAddrMgr.assignPublicIpAddress(dc.getId(), pod.getId(), vm.getOwner(), VlanType.DirectAttached, network.getId(), requiredIp, false);
        nic.setIPv4Address(ip.getAddress().toString());
        nic.setFormat(AddressFormat.Ip4);
        nic.setIPv4Gateway(ip.getGateway());
        nic.setIPv4Netmask(ip.getNetmask());
        if (ip.getVlanTag() != null && ip.getVlanTag().equalsIgnoreCase(Vlan.UNTAGGED)) {
            nic.setIsolationUri(IsolationType.Ec2.toUri(Vlan.UNTAGGED));
            nic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(Vlan.UNTAGGED));
            nic.setBroadcastType(BroadcastDomainType.Native);
        }
        nic.setReservationId(String.valueOf(ip.getVlanTag()));
        nic.setMacAddress(ip.getMacAddress());
    }
    nic.setIPv4Dns1(dc.getDns1());
    nic.setIPv4Dns2(dc.getDns2());
}
Also used : DataCenter(com.cloud.dc.DataCenter) PublicIp(com.cloud.network.addr.PublicIp)

Example 8 with PublicIp

use of com.cloud.network.addr.PublicIp in project cloudstack by apache.

the class NetworkServiceImpl method canUpgrade.

protected boolean canUpgrade(Network network, long oldNetworkOfferingId, long newNetworkOfferingId) {
    NetworkOffering oldNetworkOffering = _networkOfferingDao.findByIdIncludingRemoved(oldNetworkOfferingId);
    NetworkOffering newNetworkOffering = _networkOfferingDao.findById(newNetworkOfferingId);
    // can upgrade only Isolated networks
    if (oldNetworkOffering.getGuestType() != GuestType.Isolated) {
        throw new InvalidParameterValueException("NetworkOfferingId can be upgraded only for the network of type " + GuestType.Isolated);
    }
    // security group service should be the same
    if (areServicesSupportedByNetworkOffering(oldNetworkOfferingId, Service.SecurityGroup) != areServicesSupportedByNetworkOffering(newNetworkOfferingId, Service.SecurityGroup)) {
        s_logger.debug("Offerings " + newNetworkOfferingId + " and " + oldNetworkOfferingId + " have different securityGroupProperty, can't upgrade");
        return false;
    }
    // Type of the network should be the same
    if (oldNetworkOffering.getGuestType() != newNetworkOffering.getGuestType()) {
        s_logger.debug("Network offerings " + newNetworkOfferingId + " and " + oldNetworkOfferingId + " are of different types, can't upgrade");
        return false;
    }
    // tags should be the same
    if (newNetworkOffering.getTags() != null) {
        if (oldNetworkOffering.getTags() == null) {
            s_logger.debug("New network offering id=" + newNetworkOfferingId + " has tags and old network offering id=" + oldNetworkOfferingId + " doesn't, can't upgrade");
            return false;
        }
        if (!StringUtils.areTagsEqual(oldNetworkOffering.getTags(), newNetworkOffering.getTags())) {
            s_logger.debug("Network offerings " + newNetworkOffering.getUuid() + " and " + oldNetworkOffering.getUuid() + " have different tags, can't upgrade");
            return false;
        }
    }
    // Traffic types should be the same
    if (oldNetworkOffering.getTrafficType() != newNetworkOffering.getTrafficType()) {
        s_logger.debug("Network offerings " + newNetworkOfferingId + " and " + oldNetworkOfferingId + " have different traffic types, can't upgrade");
        return false;
    }
    // specify vlan should be the same
    if (oldNetworkOffering.getSpecifyVlan() != newNetworkOffering.getSpecifyVlan()) {
        s_logger.debug("Network offerings " + newNetworkOfferingId + " and " + oldNetworkOfferingId + " have different values for specifyVlan, can't upgrade");
        return false;
    }
    // specify ipRanges should be the same
    if (oldNetworkOffering.getSpecifyIpRanges() != newNetworkOffering.getSpecifyIpRanges()) {
        s_logger.debug("Network offerings " + newNetworkOfferingId + " and " + oldNetworkOfferingId + " have different values for specifyIpRangess, can't upgrade");
        return false;
    }
    // Check all ips
    List<IPAddressVO> userIps = _ipAddressDao.listByAssociatedNetwork(network.getId(), null);
    List<PublicIp> publicIps = new ArrayList<PublicIp>();
    if (userIps != null && !userIps.isEmpty()) {
        for (IPAddressVO userIp : userIps) {
            PublicIp publicIp = PublicIp.createFromAddrAndVlan(userIp, _vlanDao.findById(userIp.getVlanId()));
            publicIps.add(publicIp);
        }
    }
    if (oldNetworkOffering.isConserveMode() && !newNetworkOffering.isConserveMode()) {
        if (!canIpsUsedForNonConserve(publicIps)) {
            return false;
        }
    }
    //can't update from internal LB to public LB
    if (areServicesSupportedByNetworkOffering(oldNetworkOfferingId, Service.Lb) && areServicesSupportedByNetworkOffering(newNetworkOfferingId, Service.Lb)) {
        if (oldNetworkOffering.getPublicLb() != newNetworkOffering.getPublicLb() || oldNetworkOffering.getInternalLb() != newNetworkOffering.getInternalLb()) {
            throw new InvalidParameterValueException("Original and new offerings support different types of LB - Internal vs Public," + " can't upgrade");
        }
    }
    return canIpsUseOffering(publicIps, newNetworkOfferingId);
}
Also used : NetworkOffering(com.cloud.offering.NetworkOffering) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) PublicIp(com.cloud.network.addr.PublicIp) ArrayList(java.util.ArrayList) IPAddressVO(com.cloud.network.dao.IPAddressVO)

Example 9 with PublicIp

use of com.cloud.network.addr.PublicIp in project cloudstack by apache.

the class IpAddressManagerImpl method isPortableIpTransferableFromNetwork.

@Override
public boolean isPortableIpTransferableFromNetwork(long ipAddrId, long networkId) {
    Network network = _networksDao.findById(networkId);
    if (network == null) {
        throw new InvalidParameterValueException("Invalid network id is given");
    }
    IPAddressVO ip = _ipAddressDao.findById(ipAddrId);
    if (ip == null) {
        throw new InvalidParameterValueException("Invalid network id is given");
    }
    // Check if IP has any services (rules) associated in the network
    List<PublicIpAddress> ipList = new ArrayList<PublicIpAddress>();
    PublicIp publicIp = PublicIp.createFromAddrAndVlan(ip, _vlanDao.findById(ip.getVlanId()));
    ipList.add(publicIp);
    Map<PublicIpAddress, Set<Service>> ipToServices = _networkModel.getIpToServices(ipList, false, true);
    if (!ipToServices.isEmpty()) {
        Set<Service> ipServices = ipToServices.get(publicIp);
        if (ipServices != null && !ipServices.isEmpty()) {
            return false;
        }
    }
    return true;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) PublicIp(com.cloud.network.addr.PublicIp) ArrayList(java.util.ArrayList) NetworkOrchestrationService(org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService) Service(com.cloud.network.Network.Service) ResourceLimitService(com.cloud.user.ResourceLimitService) RemoteAccessVpnService(com.cloud.network.vpn.RemoteAccessVpnService) IPAddressVO(com.cloud.network.dao.IPAddressVO)

Example 10 with PublicIp

use of com.cloud.network.addr.PublicIp in project cloudstack by apache.

the class IpAddressManagerImpl method assignDedicateIpAddress.

@DB
@Override
public PublicIp assignDedicateIpAddress(Account owner, final Long guestNtwkId, final Long vpcId, final long dcId, final boolean isSourceNat) throws ConcurrentOperationException, InsufficientAddressCapacityException {
    final long ownerId = owner.getId();
    PublicIp ip = null;
    try {
        ip = Transaction.execute(new TransactionCallbackWithException<PublicIp, InsufficientAddressCapacityException>() {

            @Override
            public PublicIp doInTransaction(TransactionStatus status) throws InsufficientAddressCapacityException {
                Account owner = _accountDao.acquireInLockTable(ownerId);
                if (owner == null) {
                    // this ownerId comes from owner or type Account. See the class "AccountVO" and the annotations in that class
                    // to get the table name and field name that is queried to fill this ownerid.
                    ConcurrentOperationException ex = new ConcurrentOperationException("Unable to lock account");
                    throw ex;
                }
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("lock account " + ownerId + " is acquired");
                }
                boolean displayIp = true;
                if (guestNtwkId != null) {
                    Network ntwk = _networksDao.findById(guestNtwkId);
                    displayIp = ntwk.getDisplayNetwork();
                } else if (vpcId != null) {
                    VpcVO vpc = _vpcDao.findById(vpcId);
                    displayIp = vpc.isDisplay();
                }
                PublicIp ip = fetchNewPublicIp(dcId, null, null, owner, VlanType.VirtualNetwork, guestNtwkId, isSourceNat, false, null, false, vpcId, displayIp);
                IPAddressVO publicIp = ip.ip();
                markPublicIpAsAllocated(publicIp);
                _ipAddressDao.update(publicIp.getId(), publicIp);
                return ip;
            }
        });
        return ip;
    } finally {
        if (owner != null) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Releasing lock account " + ownerId);
            }
            _accountDao.releaseFromLockTable(ownerId);
        }
        if (ip == null) {
            s_logger.error("Unable to get source nat ip address for account " + ownerId);
        }
    }
}
Also used : Account(com.cloud.user.Account) VpcVO(com.cloud.network.vpc.VpcVO) PublicIp(com.cloud.network.addr.PublicIp) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) TransactionStatus(com.cloud.utils.db.TransactionStatus) IPAddressVO(com.cloud.network.dao.IPAddressVO) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) DB(com.cloud.utils.db.DB)

Aggregations

PublicIp (com.cloud.network.addr.PublicIp)38 IPAddressVO (com.cloud.network.dao.IPAddressVO)20 ArrayList (java.util.ArrayList)16 Network (com.cloud.network.Network)9 Account (com.cloud.user.Account)9 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)9 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)8 DataCenter (com.cloud.dc.DataCenter)7 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)6 Test (org.junit.Test)6 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)5 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)5 Service (com.cloud.network.Network.Service)5 DB (com.cloud.utils.db.DB)5 TransactionStatus (com.cloud.utils.db.TransactionStatus)5 HashSet (java.util.HashSet)5 Set (java.util.Set)5 VlanVO (com.cloud.dc.VlanVO)4 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)4 Provider (com.cloud.network.Network.Provider)4