Search in sources :

Example 26 with VlanVO

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

the class NetworkServiceImpl method releaseIpAddressInternal.

@DB
private boolean releaseIpAddressInternal(long ipAddressId) throws InsufficientAddressCapacityException {
    Long userId = CallContext.current().getCallingUserId();
    Account caller = CallContext.current().getCallingAccount();
    // Verify input parameters
    IPAddressVO ipVO = _ipAddressDao.findById(ipAddressId);
    if (ipVO == null) {
        throw new InvalidParameterValueException("Unable to find ip address by id");
    }
    if (ipVO.getAllocatedTime() == null) {
        s_logger.debug("Ip Address id= " + ipAddressId + " is not allocated, so do nothing.");
        return true;
    }
    // verify permissions
    if (ipVO.getAllocatedToAccountId() != null) {
        _accountMgr.checkAccess(caller, null, true, ipVO);
    }
    if (ipVO.isSourceNat()) {
        throw new IllegalArgumentException("ip address is used for source nat purposes and can not be disassociated.");
    }
    VlanVO vlan = _vlanDao.findById(ipVO.getVlanId());
    if (!vlan.getVlanType().equals(VlanType.VirtualNetwork)) {
        throw new IllegalArgumentException("only ip addresses that belong to a virtual network may be disassociated.");
    }
    // don't allow releasing system ip address
    if (ipVO.getSystem()) {
        InvalidParameterValueException ex = new InvalidParameterValueException("Can't release system IP address with specified id");
        ex.addProxyObject(ipVO.getUuid(), "systemIpAddrId");
        throw ex;
    }
    boolean success = _ipAddrMgr.disassociatePublicIpAddress(ipAddressId, userId, caller);
    if (success) {
        Long networkId = ipVO.getAssociatedWithNetworkId();
        if (networkId != null) {
            Network guestNetwork = getNetwork(networkId);
            NetworkOffering offering = _entityMgr.findById(NetworkOffering.class, guestNetwork.getNetworkOfferingId());
            Long vmId = ipVO.getAssociatedWithVmId();
            if (offering.getElasticIp() && vmId != null) {
                _rulesMgr.getSystemIpAndEnableStaticNatForVm(_userVmDao.findById(vmId), true);
                return true;
            }
        }
    } else {
        s_logger.warn("Failed to release public ip address id=" + ipAddressId);
    }
    return success;
}
Also used : Account(com.cloud.user.Account) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) NetworkOffering(com.cloud.offering.NetworkOffering) IPAddressVO(com.cloud.network.dao.IPAddressVO) VlanVO(com.cloud.dc.VlanVO) DB(com.cloud.utils.db.DB)

Example 27 with VlanVO

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

the class VlanDaoImpl method assignPodDirectAttachIpAddress.

public Pair<String, VlanVO> assignPodDirectAttachIpAddress(long zoneId, long podId, long accountId, long domainId) {
    SearchCriteria<VlanVO> sc = ZoneTypePodSearch.create();
    sc.setParameters("zoneId", zoneId);
    sc.setParameters("vlanType", VlanType.DirectAttached);
    sc.setJoinParameters("vlan", "podId", podId);
    VlanVO vlan = findOneIncludingRemovedBy(sc);
    if (vlan == null) {
        return null;
    }
    return null;
//        String ipAddress = _ipAddressDao.assignIpAddress(accountId, domainId, vlan.getId(), false).getAddress();
//        if (ipAddress == null) {
//            return null;
//        }
//        return new Pair<String, VlanVO>(ipAddress, vlan);
}
Also used : VlanVO(com.cloud.dc.VlanVO)

Example 28 with VlanVO

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

the class VlanDaoImpl method findNextVlan.

private VlanVO findNextVlan(long zoneId, Vlan.VlanType vlanType) {
    List<VlanVO> allVlans = listByZoneAndType(zoneId, vlanType);
    List<VlanVO> emptyVlans = new ArrayList<VlanVO>();
    List<VlanVO> fullVlans = new ArrayList<VlanVO>();
    // Try to find a VLAN that is partially allocated
    for (VlanVO vlan : allVlans) {
        long vlanDbId = vlan.getId();
        int countOfAllocatedIps = _ipAddressDao.countIPs(zoneId, vlanDbId, true);
        int countOfAllIps = _ipAddressDao.countIPs(zoneId, vlanDbId, false);
        if ((countOfAllocatedIps > 0) && (countOfAllocatedIps < countOfAllIps)) {
            return vlan;
        } else if (countOfAllocatedIps == 0) {
            emptyVlans.add(vlan);
        } else if (countOfAllocatedIps == countOfAllIps) {
            fullVlans.add(vlan);
        }
    }
    if (emptyVlans.isEmpty()) {
        return null;
    }
    // Try to find an empty VLAN with the same tag/subnet as a VLAN that is full
    for (VlanVO fullVlan : fullVlans) {
        for (VlanVO emptyVlan : emptyVlans) {
            if (fullVlan.getVlanTag().equals(emptyVlan.getVlanTag()) && fullVlan.getVlanGateway().equals(emptyVlan.getVlanGateway()) && fullVlan.getVlanNetmask().equals(emptyVlan.getVlanNetmask())) {
                return emptyVlan;
            }
        }
    }
    // Return a random empty VLAN
    return emptyVlans.get(0);
}
Also used : ArrayList(java.util.ArrayList) VlanVO(com.cloud.dc.VlanVO)

Example 29 with VlanVO

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

the class BigSwitchBcfUtils method getTopology.

public TopologyData getTopology(long physicalNetworkId) {
    List<NetworkVO> networks;
    List<NicVO> nics;
    networks = _networkDao.listByPhysicalNetworkTrafficType(physicalNetworkId, TrafficType.Guest);
    TopologyData topo = new TopologyData();
    // handle external network first, only if NAT service is enabled
    if (networks != null) {
        if (!(networks.isEmpty()) && isNatEnabled()) {
            // get public net info - needed to set up source nat gateway
            NetworkVO pubNet = getPublicNetwork(physicalNetworkId);
            // locate subnet info
            SearchCriteria<VlanVO> sc = _vlanDao.createSearchCriteria();
            sc.setParameters("network_id", pubNet.getId());
            VlanVO vlanVO = _vlanDao.findOneBy(sc);
            // add tenant external network external
            TopologyData.Network network = topo.new Network();
            network.setId("external");
            network.setName("external");
            network.setTenantId("external");
            network.setTenantName("external");
            String pubVlan = null;
            try {
                pubVlan = BroadcastDomainType.getValue(vlanVO.getVlanTag());
                if (StringUtils.isNumeric(pubVlan)) {
                    network.setVlan(Integer.valueOf(pubVlan));
                } else {
                    // untagged
                    pubVlan = "0";
                }
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
            topo.addNetwork(network);
        }
    }
    // routerMap used internally for multiple updates to same tenant's router
    // add back to topo.routers after loop
    HashMap<String, RouterData> routerMap = new HashMap<String, RouterData>();
    for (NetworkVO netVO : networks) {
        TopologyData.Network network = topo.new Network();
        network.setId(netVO.getUuid());
        network.setName(netVO.getName());
        Integer vlan = null;
        if (netVO.getBroadcastUri() != null) {
            String vlanStr = BroadcastDomainType.getValue(netVO.getBroadcastUri());
            if (StringUtils.isNumeric(vlanStr)) {
                vlan = Integer.valueOf(vlanStr);
            } else {
                // untagged
                vlan = 0;
            }
        }
        network.setVlan(vlan);
        network.setState(netVO.getState().name());
        nics = _nicDao.listByNetworkId(netVO.getId());
        List<Port> ports = new ArrayList<Port>();
        String tenantId = null;
        String tenantName = null;
        // if VPC network, assign BCF tenant id with vpc uuid
        Vpc vpc = null;
        if (netVO.getVpcId() != null) {
            vpc = _vpcDao.acquireInLockTable(netVO.getVpcId());
        }
        if (vpc != null) {
            tenantId = vpc.getUuid();
            tenantName = vpc.getName();
        } else {
            tenantId = netVO.getUuid();
            tenantName = netVO.getName();
        }
        for (NicVO nic : nics) {
            NetworkData netData = new NetworkData();
            TopologyData.Port p = topo.new Port();
            p.setAttachmentInfo(netData.new AttachmentInfo(nic.getUuid(), nic.getMacAddress()));
            VMInstanceVO vm = _vmDao.findById(nic.getInstanceId());
            HostVO host = _hostDao.findById(vm.getHostId());
            // if host not found, ignore this nic
            if (host == null) {
                continue;
            }
            String hostname = host.getName();
            long zoneId = netVO.getDataCenterId();
            String vmwareVswitchLabel = _networkModel.getDefaultGuestTrafficLabel(zoneId, HypervisorType.VMware);
            String[] labelArray = null;
            String vswitchName = null;
            if (vmwareVswitchLabel != null) {
                labelArray = vmwareVswitchLabel.split(",");
                vswitchName = labelArray[0];
            }
            // hypervisor type:
            //   kvm: ivs port name
            //   vmware: specific portgroup naming convention
            String pgName = "";
            if (host.getHypervisorType() == HypervisorType.KVM) {
                pgName = hostname;
            } else if (host.getHypervisorType() == HypervisorType.VMware) {
                pgName = hostname + "-" + vswitchName;
            }
            p.setHostId(pgName);
            p.setSegmentInfo(netData.new SegmentInfo(BroadcastDomainType.Vlan.name(), vlan));
            p.setOwner(BigSwitchBcfApi.getCloudstackInstanceId());
            List<AttachmentData.Attachment.IpAddress> ipList = new ArrayList<AttachmentData.Attachment.IpAddress>();
            ipList.add(new AttachmentData().getAttachment().new IpAddress(nic.getIPv4Address()));
            p.setIpAddresses(ipList);
            p.setId(nic.getUuid());
            p.setMac(nic.getMacAddress());
            netData.getNetwork().setId(network.getId());
            netData.getNetwork().setName(network.getName());
            netData.getNetwork().setTenantId(tenantId);
            netData.getNetwork().setTenantName(tenantName);
            netData.getNetwork().setState(netVO.getState().name());
            p.setNetwork(netData.getNetwork());
            ports.add(p);
        }
        network.setTenantId(tenantId);
        network.setTenantName(tenantName);
        network.setPorts(ports);
        topo.addNetwork(network);
        // add router for network
        RouterData routerData;
        if (tenantId != null) {
            if (!routerMap.containsKey(tenantId)) {
                routerData = new RouterData(tenantId);
                routerMap.put(tenantId, routerData);
            } else {
                routerData = routerMap.get(tenantId);
            }
            routerData.getRouter().getAcls().addAll(listACLbyNetwork(netVO));
            if (vpc != null) {
                routerData.getRouter().addExternalGateway(getPublicIpByVpc(vpc));
            } else {
                routerData.getRouter().addExternalGateway(getPublicIpByNetwork(netVO));
            }
            RouterInterfaceData intf = new RouterInterfaceData(tenantId, netVO.getGateway(), netVO.getCidr(), netVO.getUuid(), netVO.getName());
            routerData.getRouter().addInterface(intf);
        }
    }
    for (RouterData rd : routerMap.values()) {
        topo.addRouter(rd.getRouter());
    }
    return topo;
}
Also used : HashMap(java.util.HashMap) Port(com.cloud.network.bigswitch.TopologyData.Port) ArrayList(java.util.ArrayList) Vpc(com.cloud.network.vpc.Vpc) URISyntaxException(java.net.URISyntaxException) Network(com.cloud.network.Network) VlanVO(com.cloud.dc.VlanVO) NicVO(com.cloud.vm.NicVO) NetworkVO(com.cloud.network.dao.NetworkVO) Port(com.cloud.network.bigswitch.TopologyData.Port) VMInstanceVO(com.cloud.vm.VMInstanceVO) HostVO(com.cloud.host.HostVO)

Example 30 with VlanVO

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

the class BigSwitchBcfGuestNetworkGuru method implement.

@Override
public Network implement(Network network, NetworkOffering offering, DeployDestination dest, ReservationContext context) throws InsufficientVirtualNetworkCapacityException {
    assert (network.getState() == State.Implementing) : "Why are we implementing " + network;
    bcfUtilsInit();
    long dcId = dest.getDataCenter().getId();
    long physicalNetworkId = _networkModel.findPhysicalNetworkId(dcId, offering.getTags(), offering.getTrafficType());
    NetworkVO implemented = new NetworkVO(network.getTrafficType(), network.getMode(), network.getBroadcastDomainType(), network.getNetworkOfferingId(), State.Allocated, network.getDataCenterId(), physicalNetworkId, false);
    if (network.getGateway() != null) {
        implemented.setGateway(network.getGateway());
    }
    if (network.getCidr() != null) {
        implemented.setCidr(network.getCidr());
    }
    String vnetId = "";
    if (network.getBroadcastUri() == null) {
        vnetId = _dcDao.allocateVnet(dcId, physicalNetworkId, network.getAccountId(), context.getReservationId(), UseSystemGuestVlans.valueIn(network.getAccountId()));
        if (vnetId == null) {
            throw new InsufficientVirtualNetworkCapacityException("Unable to allocate vnet as a " + "part of network " + network + " implement ", DataCenter.class, dcId);
        }
        implemented.setBroadcastUri(BroadcastDomainType.Vlan.toUri(vnetId));
        ActionEventUtils.onCompletedActionEvent(CallContext.current().getCallingUserId(), network.getAccountId(), EventVO.LEVEL_INFO, EventTypes.EVENT_ZONE_VLAN_ASSIGN, "Assigned Zone Vlan: " + vnetId + " Network Id: " + network.getId(), 0);
    } else {
        implemented.setBroadcastUri(network.getBroadcastUri());
    }
    // Name is either the given name or the uuid
    String name = network.getName();
    if (name == null || name.isEmpty()) {
        name = ((NetworkVO) network).getUuid();
    }
    if (name.length() > 64) {
        // max length 64
        name = name.substring(0, 63);
    }
    // update fields in network object
    NetworkVO networkObject = (NetworkVO) network;
    // determine whether this is VPC network or stand-alone network
    Vpc vpc = null;
    if (network.getVpcId() != null) {
        vpc = _vpcDao.acquireInLockTable(network.getVpcId());
    }
    // use uuid of networkVO as network id in BSN
    String networkId = networkObject.getUuid();
    String tenantId;
    String tenantName;
    if (vpc != null) {
        tenantId = vpc.getUuid();
        tenantName = vpc.getName();
        _vpcDao.releaseFromLockTable(vpc.getId());
    } else {
        // use network in CS as tenant in BSN
        // for non-VPC networks, use network name and id as tenant name and id
        tenantId = networkId;
        tenantName = name;
    }
    // store tenantId in networkObject for NetworkOrchestrator implementNetwork use
    networkObject.setNetworkDomain(tenantId);
    // store tenant Id in implemented object for future actions (e.g., shutdown)
    implemented.setNetworkDomain(tenantId);
    String vlanStr = BroadcastDomainType.getValue(implemented.getBroadcastUri());
    // get public net info - needed to set up source nat gateway
    NetworkVO pubNet = _bcfUtils.getPublicNetwork(physicalNetworkId);
    // locate subnet info
    SearchCriteria<VlanVO> sc = _vlanDao.createSearchCriteria();
    sc.setParameters("network_id", pubNet.getId());
    VlanVO pubVlanVO = _vlanDao.findOneBy(sc);
    String pubVlanStr = pubVlanVO.getVlanTag();
    Integer vlan;
    if (StringUtils.isNumeric(vlanStr)) {
        vlan = Integer.valueOf(vlanStr);
    } else {
        vlan = 0;
    }
    CreateBcfSegmentCommand cmd1 = new CreateBcfSegmentCommand(tenantId, tenantName, networkId, name, vlan);
    _bcfUtils.sendBcfCommandWithNetworkSyncCheck(cmd1, networkObject);
    if (_bcfUtils.isNatEnabled()) {
        Integer pvlan;
        if (StringUtils.isNumeric(pubVlanStr)) {
            pvlan = Integer.valueOf(pubVlanStr);
        } else {
            pvlan = 0;
        }
        CreateBcfSegmentCommand cmd2 = new CreateBcfSegmentCommand("external", "external", "external", "external", pvlan);
        _bcfUtils.sendBcfCommandWithNetworkSyncCheck(cmd2, networkObject);
        CreateBcfRouterCommand cmd3 = new CreateBcfRouterCommand(tenantId);
        _bcfUtils.sendBcfCommandWithNetworkSyncCheck(cmd3, network);
        CreateBcfRouterInterfaceCommand cmd5 = new CreateBcfRouterInterfaceCommand(tenantId, network.getUuid(), network.getCidr(), network.getGateway(), network.getName());
        _bcfUtils.sendBcfCommandWithNetworkSyncCheck(cmd5, network);
    }
    return implemented;
}
Also used : CreateBcfSegmentCommand(com.cloud.agent.api.CreateBcfSegmentCommand) CreateBcfRouterCommand(com.cloud.agent.api.CreateBcfRouterCommand) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) CreateBcfRouterInterfaceCommand(com.cloud.agent.api.CreateBcfRouterInterfaceCommand) InsufficientVirtualNetworkCapacityException(com.cloud.exception.InsufficientVirtualNetworkCapacityException) Vpc(com.cloud.network.vpc.Vpc) VlanVO(com.cloud.dc.VlanVO)

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