Search in sources :

Example 6 with PrivateIpVO

use of com.cloud.network.vpc.PrivateIpVO in project cosmic by MissionCriticalCloud.

the class PrivateIpDaoImpl method allocateIpAddress.

@Override
public PrivateIpVO allocateIpAddress(final long dcId, final long networkId, final Long vpcId, final String requestedIp) {
    final SearchCriteria<PrivateIpVO> sc = AllFieldsSearch.create();
    sc.setParameters("networkId", networkId);
    if (vpcId != null) {
        sc.setParameters("vpc_id", vpcId);
    }
    if (requestedIp != null) {
        sc.setParameters("ipAddress", requestedIp);
    }
    final TransactionLegacy txn = TransactionLegacy.currentTxn();
    txn.start();
    final PrivateIpVO vo = lockOneRandomRow(sc, true);
    if (vo == null) {
        txn.rollback();
        return null;
    }
    vo.setTakenAt(new Date());
    update(vo.getId(), vo);
    txn.commit();
    return vo;
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) PrivateIpVO(com.cloud.network.vpc.PrivateIpVO) Date(java.util.Date)

Example 7 with PrivateIpVO

use of com.cloud.network.vpc.PrivateIpVO in project cosmic by MissionCriticalCloud.

the class PrivateNetworkGuru method getIp.

protected void getIp(final NicProfile nic, final DataCenter dc, final Network network) throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException {
    if (nic.getIPv4Address() == null) {
        final PrivateIpVO ipVO = _privateIpDao.allocateIpAddress(network.getDataCenterId(), network.getId(), network.getVpcId(), null);
        final String vlanTag = BroadcastDomainType.getValue(network.getBroadcastUri());
        final String netmask = NetUtils.getCidrNetmask(network.getCidr());
        final PrivateIpAddress ip = new PrivateIpAddress(ipVO, vlanTag, network.getGateway(), netmask, NetUtils.long2Mac(NetUtils.createSequenceBasedMacAddress(ipVO.getMacAddress())));
        nic.setIPv4Address(ip.getIpAddress());
        nic.setIPv4Gateway(ip.getGateway());
        nic.setIPv4Netmask(ip.getNetmask());
        nic.setIsolationUri(IsolationType.Vlan.toUri(ip.getBroadcastUri()));
        nic.setBroadcastUri(IsolationType.Vlan.toUri(ip.getBroadcastUri()));
        nic.setBroadcastType(BroadcastDomainType.Vlan);
        nic.setFormat(AddressFormat.Ip4);
        nic.setReservationId(String.valueOf(ip.getBroadcastUri()));
        nic.setMacAddress(ip.getMacAddress());
    }
    nic.setIPv4Dns1(dc.getDns1());
    nic.setIPv4Dns2(dc.getDns2());
}
Also used : PrivateIpAddress(com.cloud.network.vpc.PrivateIpAddress) PrivateIpVO(com.cloud.network.vpc.PrivateIpVO)

Example 8 with PrivateIpVO

use of com.cloud.network.vpc.PrivateIpVO in project cosmic by MissionCriticalCloud.

the class NicProfileHelperImpl method createPrivateNicProfileForGateway.

@Override
@DB
public NicProfile createPrivateNicProfileForGateway(final VpcGateway privateGateway, final VirtualRouter router) {
    final Network privateNetwork = _networkModel.getNetwork(privateGateway.getNetworkId());
    final Long vpcId = privateGateway.getVpcId();
    final Vpc activeVpc = _vpcMgr.getActiveVpc(vpcId);
    PrivateIpVO ipVO = _privateIpDao.allocateIpAddress(privateNetwork.getDataCenterId(), privateNetwork.getId(), vpcId, privateGateway.getIp4Address());
    if (activeVpc.isRedundant() && ipVO == null) {
        ipVO = _privateIpDao.findByIpAndVpcId(vpcId, privateGateway.getIp4Address());
    }
    Nic privateNic = null;
    if (ipVO != null) {
        privateNic = _nicDao.findByIp4AddressAndNetworkId(ipVO.getIpAddress(), privateNetwork.getId());
    }
    NicProfile privateNicProfile = new NicProfile();
    if (privateNic != null) {
        privateNicProfile = new NicProfile(privateNic, privateNetwork, privateNic.getBroadcastUri(), privateNic.getIsolationUri(), _networkModel.getNetworkRate(privateNetwork.getId(), router.getId()), _networkModel.getNetworkTag(router.getHypervisorType(), privateNetwork));
        if (router.getIsRedundantRouter()) {
            final String newMacAddress = NetUtils.long2Mac(NetUtils.createSequenceBasedMacAddress(ipVO.getMacAddress()));
            privateNicProfile.setMacAddress(newMacAddress);
        }
    } else {
        final String netmask = NetUtils.getCidrNetmask(privateNetwork.getCidr());
        final PrivateIpAddress ip = new PrivateIpAddress(ipVO, privateNetwork.getBroadcastUri().toString(), privateNetwork.getGateway(), netmask, NetUtils.long2Mac(NetUtils.createSequenceBasedMacAddress(ipVO.getMacAddress())));
        final URI netUri = BroadcastDomainType.fromString(ip.getBroadcastUri());
        privateNicProfile.setIPv4Address(ip.getIpAddress());
        privateNicProfile.setIPv4Gateway(ip.getGateway());
        privateNicProfile.setIPv4Netmask(ip.getNetmask());
        privateNicProfile.setIsolationUri(netUri);
        privateNicProfile.setBroadcastUri(netUri);
        // can we solve this in setBroadcastUri()???
        // or more plugable construct is desirable
        privateNicProfile.setBroadcastType(BroadcastDomainType.getSchemeValue(netUri));
        privateNicProfile.setFormat(AddressFormat.Ip4);
        privateNicProfile.setReservationId(String.valueOf(ip.getBroadcastUri()));
        privateNicProfile.setMacAddress(ip.getMacAddress());
    }
    return privateNicProfile;
}
Also used : PrivateIpAddress(com.cloud.network.vpc.PrivateIpAddress) Network(com.cloud.network.Network) Vpc(com.cloud.network.vpc.Vpc) Nic(com.cloud.vm.Nic) PrivateIpVO(com.cloud.network.vpc.PrivateIpVO) NicProfile(com.cloud.vm.NicProfile) URI(java.net.URI) DB(com.cloud.utils.db.DB)

Example 9 with PrivateIpVO

use of com.cloud.network.vpc.PrivateIpVO in project cosmic by MissionCriticalCloud.

the class NetworkServiceImpl method createPrivateNetwork.

@Override
@DB
public Network createPrivateNetwork(final String networkName, final String displayText, final long physicalNetworkId, final String broadcastUriString, final String startIp, String endIp, final String gateway, final String netmask, final long networkOwnerId, final Long vpcId, final Boolean sourceNat, final Long networkOfferingId) throws ResourceAllocationException, ConcurrentOperationException, InsufficientCapacityException {
    final Account owner = _accountMgr.getAccount(networkOwnerId);
    // Get system network offering
    NetworkOfferingVO ntwkOff = null;
    if (networkOfferingId != null) {
        ntwkOff = _networkOfferingDao.findById(networkOfferingId);
    }
    if (ntwkOff == null) {
        ntwkOff = findSystemNetworkOffering(NetworkOffering.DefaultPrivateGatewayNetworkOffering);
    }
    // Validate physical network
    final PhysicalNetwork pNtwk = _physicalNetworkDao.findById(physicalNetworkId);
    if (pNtwk == null) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find a physical network" + " having the given id");
        ex.addProxyObject(String.valueOf(physicalNetworkId), "physicalNetworkId");
        throw ex;
    }
    // if end ip is not specified, default it to startIp
    if (!NetUtils.isValidIp4(startIp)) {
        throw new InvalidParameterValueException("Invalid format for the ip address parameter");
    }
    if (endIp == null) {
        endIp = startIp;
    } else if (!NetUtils.isValidIp4(endIp)) {
        throw new InvalidParameterValueException("Invalid format for the endIp address parameter");
    }
    if (!NetUtils.isValidIp4(gateway)) {
        throw new InvalidParameterValueException("Invalid gateway");
    }
    if (!NetUtils.isValidIp4Netmask(netmask)) {
        throw new InvalidParameterValueException("Invalid netmask");
    }
    final String cidr = NetUtils.ipAndNetMaskToCidr(gateway, netmask);
    final URI uri = BroadcastDomainType.fromString(broadcastUriString);
    final String uriString = uri.toString();
    final BroadcastDomainType tiep = BroadcastDomainType.getSchemeValue(uri);
    // TODO make a test for any supported scheme
    if (!(tiep == BroadcastDomainType.Vlan || tiep == BroadcastDomainType.Lswitch)) {
        throw new InvalidParameterValueException("unsupported type of broadcastUri specified: " + broadcastUriString);
    }
    final NetworkOfferingVO ntwkOffFinal = ntwkOff;
    try {
        return Transaction.execute(new TransactionCallbackWithException<Network, Exception>() {

            @Override
            public Network doInTransaction(final TransactionStatus status) throws ResourceAllocationException, InsufficientCapacityException {
                // lock datacenter as we need to get mac address seq from there
                final DataCenterVO dc = _dcDao.lockRow(pNtwk.getDataCenterId(), true);
                // check if we need to create guest network
                Network privateNetwork = _networksDao.getPrivateNetwork(uriString, cidr, networkOwnerId, pNtwk.getDataCenterId(), networkOfferingId);
                if (privateNetwork == null) {
                    // create Guest network
                    privateNetwork = _networkMgr.createGuestNetwork(ntwkOffFinal.getId(), networkName, displayText, gateway, cidr, uriString, null, owner, null, pNtwk, pNtwk.getDataCenterId(), ACLType.Account, null, vpcId, null, null, true, null, dc.getDns1(), dc.getDns2(), null);
                    if (privateNetwork != null) {
                        s_logger.debug("Successfully created guest network " + privateNetwork);
                    } else {
                        throw new CloudRuntimeException("Creating guest network failed");
                    }
                } else {
                    s_logger.debug("Private network already exists: " + privateNetwork);
                    // Do not allow multiple private gateways with same Vlan within a VPC
                    if (vpcId != null && vpcId.equals(privateNetwork.getVpcId())) {
                        throw new InvalidParameterValueException("Private network for the vlan: " + uriString + " and cidr  " + cidr + "  already exists " + "for Vpc " + vpcId + " in zone " + _entityMgr.findById(DataCenter.class, pNtwk.getDataCenterId()).getName());
                    }
                }
                if (vpcId != null) {
                    // add entry to private_ip_address table
                    PrivateIpVO privateIp = _privateIpDao.findByIpAndSourceNetworkIdAndVpcId(privateNetwork.getId(), startIp, vpcId);
                    if (privateIp != null) {
                        throw new InvalidParameterValueException("Private ip address " + startIp + " already used for private gateway" + " in zone " + _entityMgr.findById(DataCenter.class, pNtwk.getDataCenterId()).getName());
                    }
                    final Long mac = dc.getMacAddress();
                    final Long nextMac = mac + 1;
                    dc.setMacAddress(nextMac);
                    privateIp = new PrivateIpVO(startIp, privateNetwork.getId(), nextMac, vpcId, sourceNat);
                    _privateIpDao.persist(privateIp);
                    _dcDao.update(dc.getId(), dc);
                }
                s_logger.debug("Private network " + privateNetwork + " is created");
                return privateNetwork;
            }
        });
    } catch (final Exception e) {
        ExceptionUtil.rethrowRuntime(e);
        ExceptionUtil.rethrow(e, ResourceAllocationException.class);
        ExceptionUtil.rethrow(e, InsufficientCapacityException.class);
        throw new IllegalStateException(e);
    }
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) Account(com.cloud.user.Account) TransactionStatus(com.cloud.utils.db.TransactionStatus) PrivateIpVO(com.cloud.network.vpc.PrivateIpVO) URI(java.net.URI) InvalidParameterException(java.security.InvalidParameterException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SQLException(java.sql.SQLException) UnknownHostException(java.net.UnknownHostException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) UnsupportedServiceException(com.cloud.exception.UnsupportedServiceException) ConfigurationException(javax.naming.ConfigurationException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) BroadcastDomainType(com.cloud.network.Networks.BroadcastDomainType) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NetworkOfferingVO(com.cloud.offerings.NetworkOfferingVO) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) DB(com.cloud.utils.db.DB)

Example 10 with PrivateIpVO

use of com.cloud.network.vpc.PrivateIpVO in project cosmic by MissionCriticalCloud.

the class VpcVirtualNetworkApplianceManagerImpl method setupVpcPrivateNetwork.

/**
 * @param router
 * @param add
 * @param privateNic
 * @return
 * @throws ResourceUnavailableException
 */
protected boolean setupVpcPrivateNetwork(final VirtualRouter router, final boolean add, final NicProfile privateNic) throws ResourceUnavailableException {
    if (router.getState() == State.Running) {
        final PrivateIpVO ipVO = _privateIpDao.findByIpAndSourceNetworkId(privateNic.getNetworkId(), privateNic.getIPv4Address());
        final Network network = _networkDao.findById(privateNic.getNetworkId());
        final String netmask = NetUtils.getCidrNetmask(network.getCidr());
        String broadcastUri = "";
        if (network.getBroadcastUri() != null) {
            broadcastUri = network.getBroadcastUri().toString();
        }
        final PrivateIpAddress ip = new PrivateIpAddress(ipVO, broadcastUri, network.getGateway(), netmask, privateNic.getMacAddress());
        final Commands cmds = new Commands(Command.OnError.Stop);
        final List<Ip> ipsToExclude = new ArrayList<>();
        if (!add) {
            ipsToExclude.add(new Ip(ip.getIpAddress()));
        }
        final NetworkOverviewTO networkOverview = _commandSetupHelper.createNetworkOverviewFromRouter(router, new ArrayList<>(), ipsToExclude, new ArrayList<>(), null, null);
        final UpdateNetworkOverviewCommand updateNetworkOverviewCommand = _commandSetupHelper.createUpdateNetworkOverviewCommand(router, networkOverview);
        cmds.addCommand(updateNetworkOverviewCommand);
        try {
            if (_nwHelper.sendCommandsToRouter(router, cmds)) {
                s_logger.debug("Successfully applied ip association for ip " + ip + " in vpc network " + network);
                return true;
            } else {
                s_logger.warn("Failed to associate ip address " + ip + " in vpc network " + network);
                return false;
            }
        } catch (final Exception ex) {
            s_logger.warn("Failed to send  " + (add ? "add " : "delete ") + " private network " + network + " commands to rotuer ");
            return false;
        }
    } else if (router.getState() == State.Stopped || router.getState() == State.Stopping) {
        s_logger.debug("Router " + router.getInstanceName() + " is in " + router.getState() + ", so not sending setup private network command to the backend");
    } else {
        s_logger.warn("Unable to setup private gateway, virtual router " + router + " is not in the right state " + router.getState());
        throw new ResourceUnavailableException("Unable to setup Private gateway on the backend," + " virtual router " + router + " is not in the right state", DataCenter.class, router.getDataCenterId());
    }
    return true;
}
Also used : PrivateIpAddress(com.cloud.network.vpc.PrivateIpAddress) Ip(com.cloud.utils.net.Ip) PublicIp(com.cloud.network.addr.PublicIp) ArrayList(java.util.ArrayList) PrivateIpVO(com.cloud.network.vpc.PrivateIpVO) UpdateNetworkOverviewCommand(com.cloud.agent.api.UpdateNetworkOverviewCommand) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataCenter(com.cloud.dc.DataCenter) Network(com.cloud.network.Network) Commands(com.cloud.agent.manager.Commands) NetworkOverviewTO(com.cloud.agent.api.to.overviews.NetworkOverviewTO) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException)

Aggregations

PrivateIpVO (com.cloud.network.vpc.PrivateIpVO)18 PrivateIpAddress (com.cloud.network.vpc.PrivateIpAddress)9 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)8 Network (com.cloud.network.Network)8 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)7 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)6 NicProfile (com.cloud.vm.NicProfile)6 ArrayList (java.util.ArrayList)6 ConfigurationException (javax.naming.ConfigurationException)6 Commands (com.cloud.agent.manager.Commands)4 DataCenter (com.cloud.dc.DataCenter)4 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)4 DB (com.cloud.utils.db.DB)4 Nic (com.cloud.vm.Nic)4 URI (java.net.URI)4 UpdateNetworkOverviewCommand (com.cloud.agent.api.UpdateNetworkOverviewCommand)3 NetworkOverviewTO (com.cloud.agent.api.to.overviews.NetworkOverviewTO)3 PublicIp (com.cloud.network.addr.PublicIp)3 Ip (com.cloud.utils.net.Ip)3 Command (com.cloud.agent.api.Command)2