Search in sources :

Example 16 with PrivateIpVO

use of com.cloud.network.vpc.PrivateIpVO in project cloudstack by apache.

the class NicProfileHelperImpl method createPrivateNicProfileForGateway.

@Override
@DB
public NicProfile createPrivateNicProfileForGateway(final VpcGateway privateGateway, final VirtualRouter router) {
    final Network privateNetwork = _networkModel.getNetwork(privateGateway.getNetworkId());
    PrivateIpVO ipVO = _privateIpDao.allocateIpAddress(privateNetwork.getDataCenterId(), privateNetwork.getId(), privateGateway.getIp4Address());
    final Long vpcId = privateGateway.getVpcId();
    if (ipVO == null) {
        ipVO = _privateIpDao.findByIpAndVpcId(vpcId, privateGateway.getIp4Address());
        if (ipVO == null) {
            throw new CloudRuntimeException("cannot find IP address " + privateGateway.getIp4Address() + " to reuse for private gateway on vpc (id==" + vpcId + ")");
        }
    }
    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.isSecurityGroupSupportedInNetwork(privateNetwork), _networkModel.getNetworkTag(router.getHypervisorType(), privateNetwork));
        privateNicProfile.setDeviceId(null);
        if (router.getIsRedundantRouter()) {
            String newMacAddress = NetUtils.long2Mac(NetUtils.createSequenceBasedMacAddress(ipVO.getMacAddress(), NetworkModel.MACIdentifier.value()));
            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(), NetworkModel.MACIdentifier.value())));
        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) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.cloud.network.Network) 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 17 with PrivateIpVO

use of com.cloud.network.vpc.PrivateIpVO in project cloudstack by apache.

the class AdvancedNetworkVisitor method visit.

@Override
public boolean visit(final PrivateGatewayRules privateGW) throws ResourceUnavailableException {
    final VirtualRouter router = privateGW.getRouter();
    final NicProfile nicProfile = privateGW.getNicProfile();
    final boolean isAddOperation = privateGW.isAddOperation();
    if (router.getState() == State.Running) {
        final PrivateIpVO ipVO = privateGW.retrivePrivateIP(this);
        final Network network = privateGW.retrievePrivateNetwork(this);
        final String netmask = NetUtils.getCidrNetmask(network.getCidr());
        final PrivateIpAddress ip = new PrivateIpAddress(ipVO, network.getBroadcastUri().toString(), network.getGateway(), netmask, nicProfile.getMacAddress());
        final List<PrivateIpAddress> privateIps = new ArrayList<PrivateIpAddress>(1);
        privateIps.add(ip);
        final Commands cmds = new Commands(Command.OnError.Stop);
        _commandSetupHelper.createVpcAssociatePrivateIPCommands(router, privateIps, cmds, isAddOperation);
        try {
            if (_networkGeneralHelper.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  " + (isAddOperation ? "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) ArrayList(java.util.ArrayList) PrivateIpVO(com.cloud.network.vpc.PrivateIpVO) NicProfile(com.cloud.vm.NicProfile) VirtualRouter(com.cloud.network.router.VirtualRouter) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) DataCenter(com.cloud.dc.DataCenter) Network(com.cloud.network.Network) Commands(com.cloud.agent.manager.Commands) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException)

Example 18 with PrivateIpVO

use of com.cloud.network.vpc.PrivateIpVO in project cloudstack by apache.

the class PrivateIpDaoImpl method releaseIpAddress.

@Override
public void releaseIpAddress(String ipAddress, long networkId) {
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Releasing private ip address: " + ipAddress + " network id " + networkId);
    }
    SearchCriteria<PrivateIpVO> sc = AllFieldsSearch.create();
    sc.setParameters("ip", ipAddress);
    sc.setParameters("networkId", networkId);
    PrivateIpVO vo = createForUpdate();
    vo.setTakenAt(null);
    update(vo, sc);
}
Also used : PrivateIpVO(com.cloud.network.vpc.PrivateIpVO)

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