Search in sources :

Example 11 with VlanVO

use of com.cloud.dc.VlanVO 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 12 with VlanVO

use of com.cloud.dc.VlanVO in project cloudstack by apache.

the class NuageVspEntityBuilder method buildVspNetwork.

public VspNetwork buildVspNetwork(long domainId, Network network) {
    VspNetwork.Builder vspNetworkBuilder = new VspNetwork.Builder().id(network.getId()).uuid(network.getUuid()).name(network.getName()).cidr(network.getCidr()).gateway(network.getGateway());
    DomainVO domain = _domainDao.findById(domainId);
    VspDomain vspDomain = buildVspDomain(domain);
    vspNetworkBuilder.domain(vspDomain);
    AccountVO account = _accountDao.findById(network.getAccountId());
    if (account != null) {
        vspNetworkBuilder.accountUuid(account.getUuid()).accountName(account.getAccountName());
    }
    NetworkOfferingVO networkOffering = _networkOfferingDao.findById(network.getNetworkOfferingId());
    vspNetworkBuilder.egressDefaultPolicy(networkOffering.getEgressDefaultPolicy()).publicAccess(networkOffering.getSupportsPublicAccess());
    if (network.getVpcId() != null) {
        VpcVO vpc = _vpcDao.findById(network.getVpcId());
        vspNetworkBuilder.vpcUuid(vpc.getUuid()).vpcName(vpc.getName()).networkType(VspNetwork.NetworkType.Vpc);
    } else {
        if (networkOffering.getGuestType() == Network.GuestType.Shared) {
            List<VlanVO> vlans = _vlanDao.listVlansByNetworkIdIncludingRemoved(network.getId());
            List<VspAddressRange> vspAddressRanges = Lists.transform(vlans, new Function<VlanVO, VspAddressRange>() {

                @Nullable
                @Override
                public VspAddressRange apply(VlanVO vlanVO) {
                    return new VspAddressRange.Builder().gateway(vlanVO.getVlanGateway()).netmask(vlanVO.getVlanNetmask()).build();
                }
            });
            vspNetworkBuilder.networkType(VspNetwork.NetworkType.Shared).addressRanges(vspAddressRanges);
        } else if (_networkOfferingServiceMapDao.areServicesSupportedByNetworkOffering(network.getNetworkOfferingId(), Network.Service.SourceNat) || _networkOfferingServiceMapDao.areServicesSupportedByNetworkOffering(network.getNetworkOfferingId(), Network.Service.StaticNat)) {
            vspNetworkBuilder.networkType(VspNetwork.NetworkType.L3);
        } else {
            vspNetworkBuilder.networkType(VspNetwork.NetworkType.L2);
        }
    }
    boolean firewallServiceSupported = _networkModel.areServicesSupportedByNetworkOffering(network.getNetworkOfferingId(), Network.Service.Firewall);
    vspNetworkBuilder.firewallServiceSupported(firewallServiceSupported);
    String preConfiguredDomainTemplateName = NuageVspUtil.getPreConfiguredDomainTemplateName(_configurationDao, _networkDetailsDao, network, networkOffering);
    vspNetworkBuilder.domainTemplateName(preConfiguredDomainTemplateName);
    if (usesVirtualRouter(networkOffering.getId())) {
        try {
            List<Pair<String, String>> ipAddressRanges = networkOffering.getGuestType() == Network.GuestType.Shared ? getSharedIpAddressRanges(network.getId()) : getIpAddressRanges(network);
            String virtualRouterIp = getVirtualRouterIP(network, ipAddressRanges);
            vspNetworkBuilder.virtualRouterIp(virtualRouterIp);
        } catch (InsufficientVirtualNetworkCapacityException ex) {
            s_logger.error("There is an insufficient network capacity in network " + network.getId(), ex);
            throw new CloudRuntimeException("There is an insufficient network capacity in network " + network.getId(), ex);
        }
    }
    return vspNetworkBuilder.build();
}
Also used : VspDomain(net.nuage.vsp.acs.client.api.model.VspDomain) VspAddressRange(net.nuage.vsp.acs.client.api.model.VspAddressRange) AccountVO(com.cloud.user.AccountVO) DomainVO(com.cloud.domain.DomainVO) VpcVO(com.cloud.network.vpc.VpcVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InsufficientVirtualNetworkCapacityException(com.cloud.exception.InsufficientVirtualNetworkCapacityException) NetworkOfferingVO(com.cloud.offerings.NetworkOfferingVO) VspNetwork(net.nuage.vsp.acs.client.api.model.VspNetwork) VlanVO(com.cloud.dc.VlanVO) Nullable(javax.annotation.Nullable) Pair(net.nuage.vsp.acs.client.common.model.Pair)

Example 13 with VlanVO

use of com.cloud.dc.VlanVO in project cloudstack by apache.

the class NuageVspEntityBuilder method updateVspNetworkByPublicIp.

public VspNetwork updateVspNetworkByPublicIp(VspNetwork vspNetwork, Network network, String publicIp) {
    List<VlanVO> vlans = _vlanDao.listVlansByNetworkId(network.getId());
    final long ip = NetUtils.ip2Long(publicIp);
    VlanVO matchingVlan = Iterables.find(vlans, new Predicate<VlanVO>() {

        @Override
        public boolean apply(@Nullable VlanVO vlan) {
            Pair<String, String> ipAddressRange = getIpAddressRange(vlan);
            long startIp = NetUtils.ip2Long(ipAddressRange.getLeft());
            long endIp = NetUtils.ip2Long(ipAddressRange.getRight());
            return startIp <= ip && ip <= endIp;
        }
    });
    return new VspNetwork.Builder().fromObject(vspNetwork).gateway(matchingVlan.getVlanGateway()).cidr(NetUtils.getCidrFromGatewayAndNetmask(matchingVlan.getVlanGateway(), matchingVlan.getVlanNetmask())).build();
}
Also used : VlanVO(com.cloud.dc.VlanVO) Pair(net.nuage.vsp.acs.client.common.model.Pair)

Example 14 with VlanVO

use of com.cloud.dc.VlanVO in project cloudstack by apache.

the class NuageVspElement method applyStaticNats.

@Override
public boolean applyStaticNats(Network config, List<? extends StaticNat> rules) throws ResourceUnavailableException {
    List<VspStaticNat> vspStaticNatDetails = new ArrayList<VspStaticNat>();
    for (StaticNat staticNat : rules) {
        IPAddressVO sourceNatIp = _ipAddressDao.findById(staticNat.getSourceIpAddressId());
        VlanVO sourceNatVlan = _vlanDao.findById(sourceNatIp.getVlanId());
        checkVlanUnderlayCompatibility(sourceNatVlan);
        NicVO nicVO = _nicDao.findByIp4AddressAndNetworkId(staticNat.getDestIpAddress(), staticNat.getNetworkId());
        VspStaticNat vspStaticNat = _nuageVspEntityBuilder.buildVspStaticNat(staticNat.isForRevoke(), sourceNatIp, sourceNatVlan, nicVO);
        vspStaticNatDetails.add(vspStaticNat);
    }
    VspNetwork vspNetwork = _nuageVspEntityBuilder.buildVspNetwork(config);
    HostVO nuageVspHost = _nuageVspManager.getNuageVspHost(config.getPhysicalNetworkId());
    ApplyStaticNatVspCommand cmd = new ApplyStaticNatVspCommand(vspNetwork, vspStaticNatDetails);
    Answer answer = _agentMgr.easySend(nuageVspHost.getId(), cmd);
    if (answer == null || !answer.getResult()) {
        s_logger.error("ApplyStaticNatNuageVspCommand for network " + config.getUuid() + " failed on Nuage VSD " + nuageVspHost.getDetail("hostname"));
        if ((null != answer) && (null != answer.getDetails())) {
            throw new ResourceUnavailableException(answer.getDetails(), Network.class, config.getId());
        }
    }
    return true;
}
Also used : Answer(com.cloud.agent.api.Answer) ApplyStaticNatVspCommand(com.cloud.agent.api.element.ApplyStaticNatVspCommand) ArrayList(java.util.ArrayList) VspStaticNat(net.nuage.vsp.acs.client.api.model.VspStaticNat) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) IPAddressVO(com.cloud.network.dao.IPAddressVO) VlanVO(com.cloud.dc.VlanVO) VspNetwork(net.nuage.vsp.acs.client.api.model.VspNetwork) NicVO(com.cloud.vm.NicVO) HostVO(com.cloud.host.HostVO) VspStaticNat(net.nuage.vsp.acs.client.api.model.VspStaticNat) StaticNat(com.cloud.network.rules.StaticNat)

Example 15 with VlanVO

use of com.cloud.dc.VlanVO in project cloudstack by apache.

the class NuageVspEntityBuilder method getSharedIpAddressRanges.

private List<Pair<String, String>> getSharedIpAddressRanges(long networkId) {
    List<VlanVO> vlans = _vlanDao.listVlansByNetworkId(networkId);
    List<Pair<String, String>> ipAddressRanges = Lists.newArrayList();
    for (VlanVO vlan : vlans) {
        Pair<String, String> ipAddressRange = getIpAddressRange(vlan);
        if (ipAddressRange != null) {
            ipAddressRanges.add(ipAddressRange);
        }
    }
    return ipAddressRanges;
}
Also used : VlanVO(com.cloud.dc.VlanVO) Pair(net.nuage.vsp.acs.client.common.model.Pair)

Aggregations

VlanVO (com.cloud.dc.VlanVO)58 ArrayList (java.util.ArrayList)22 IPAddressVO (com.cloud.network.dao.IPAddressVO)15 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)14 DB (com.cloud.utils.db.DB)14 HostVO (com.cloud.host.HostVO)11 Network (com.cloud.network.Network)11 Account (com.cloud.user.Account)11 TransactionStatus (com.cloud.utils.db.TransactionStatus)11 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)11 List (java.util.List)8 Answer (com.cloud.agent.api.Answer)7 AccountVlanMapVO (com.cloud.dc.AccountVlanMapVO)7 DataCenter (com.cloud.dc.DataCenter)7 PublicIp (com.cloud.network.addr.PublicIp)7 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)6 NetworkVO (com.cloud.network.dao.NetworkVO)6 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)6 NetworkOffering (com.cloud.offering.NetworkOffering)6 TransactionCallbackNoReturn (com.cloud.utils.db.TransactionCallbackNoReturn)6