Search in sources :

Example 1 with VlanType

use of com.cloud.legacymodel.dc.Vlan.VlanType in project cosmic by MissionCriticalCloud.

the class ConfigurationManagerImpl method createVlanAndPublicIpRange.

@Override
@DB
public Vlan createVlanAndPublicIpRange(final long zoneId, final long networkId, final long physicalNetworkId, final boolean forVirtualNetwork, final Long podId, final String startIP, final String endIP, final String vlanGateway, final String vlanNetmask, String vlanId, final Domain domain, final Account vlanOwner, final String startIPv6, final String endIPv6, final String vlanIp6Gateway, final String vlanIp6Cidr) {
    final Network network = _networkModel.getNetwork(networkId);
    boolean ipv4 = false, ipv6 = false;
    if (startIP != null) {
        ipv4 = true;
    }
    if (startIPv6 != null) {
        ipv6 = true;
    }
    if (!ipv4 && !ipv6) {
        throw new InvalidParameterValueException("Please specify IPv4 or IPv6 address.");
    }
    // Validate the zone
    final Zone zone = zoneRepository.findById(zoneId).orElse(null);
    if (zone == null) {
        throw new InvalidParameterValueException("Please specify a valid zone.");
    }
    // ACL check
    checkZoneAccess(CallContext.current().getCallingAccount(), zone);
    // Validate the physical network
    if (_physicalNetworkDao.findById(physicalNetworkId) == null) {
        throw new InvalidParameterValueException("Please specify a valid physical network id");
    }
    // Validate the pod
    if (podId != null) {
        final Pod pod = _podDao.findById(podId);
        if (pod == null) {
            throw new InvalidParameterValueException("Please specify a valid pod.");
        }
        if (pod.getDataCenterId() != zoneId) {
            throw new InvalidParameterValueException("Pod id=" + podId + " doesn't belong to zone id=" + zoneId);
        }
        // pod vlans can be created in basic zone only
        if (zone.getNetworkType() != NetworkType.Basic || network.getTrafficType() != TrafficType.Guest) {
            throw new InvalidParameterValueException("Pod id can be specified only for the networks of type " + TrafficType.Guest + " in zone of type " + NetworkType.Basic);
        }
    }
    // 2) if vlan is missing, default it to the guest network's vlan
    if (network.getTrafficType() == TrafficType.Guest) {
        String networkVlanId = null;
        final URI uri = network.getBroadcastUri();
        if (uri != null) {
            final String[] vlan = uri.toString().split("vlan:\\/\\/");
            networkVlanId = vlan[1];
            // For pvlan
            networkVlanId = networkVlanId.split("-")[0];
        }
        if (vlanId != null) {
            // network's vlanId
            if (networkVlanId != null && !NetUtils.isSameIsolationId(networkVlanId, vlanId)) {
                throw new InvalidParameterValueException("Vlan doesn't match vlan of the network");
            }
        } else {
            vlanId = networkVlanId;
        }
    } else if (network.getTrafficType() == TrafficType.Public && vlanId == null) {
        throw new InvalidParameterValueException("Unable to determine vlan id or untagged vlan for public network");
    }
    if (vlanId == null) {
        vlanId = Vlan.UNTAGGED;
    }
    final VlanType vlanType = forVirtualNetwork ? VlanType.VirtualNetwork : VlanType.DirectAttached;
    if ((domain != null || vlanOwner != null) && zone.getNetworkType() != NetworkType.Advanced) {
        throw new InvalidParameterValueException("Vlan owner can be defined only in the zone of type " + NetworkType.Advanced);
    }
    if (ipv4) {
        // Make sure the gateway is valid
        if (!NetUtils.isValidIp4(vlanGateway)) {
            throw new InvalidParameterValueException("Please specify a valid gateway");
        }
        // Make sure the netmask is valid
        if (!NetUtils.isValidIp4Netmask(vlanNetmask)) {
            throw new InvalidParameterValueException("Please specify a valid netmask");
        }
    }
    if (ipv6) {
        if (!NetUtils.isValidIp6(vlanIp6Gateway)) {
            throw new InvalidParameterValueException("Please specify a valid IPv6 gateway");
        }
        if (!NetUtils.isValidIp6Cidr(vlanIp6Cidr)) {
            throw new InvalidParameterValueException("Please specify a valid IPv6 CIDR");
        }
    }
    if (ipv4) {
        final String newCidr = NetUtils.getCidrFromGatewayAndNetmask(vlanGateway, vlanNetmask);
        // Make sure start and end ips are with in the range of cidr calculated for this gateway and netmask {
        if (!NetUtils.isIpWithtInCidrRange(vlanGateway, newCidr) || !NetUtils.isIpWithtInCidrRange(startIP, newCidr) || !NetUtils.isIpWithtInCidrRange(endIP, newCidr)) {
            throw new InvalidParameterValueException("Please specify a valid IP range or valid netmask or valid gateway");
        }
        // Check if the new VLAN's subnet conflicts with the guest network
        // in
        // the specified zone (guestCidr is null for basic zone)
        final String guestNetworkCidr = zone.getGuestNetworkCidr();
        if (guestNetworkCidr != null) {
            if (NetUtils.isNetworksOverlap(newCidr, guestNetworkCidr)) {
                throw new InvalidParameterValueException("The new IP range you have specified has  overlapped with the guest network in zone: " + zone.getName() + ". Please specify a different gateway/netmask.");
            }
        }
        // Check if there are any errors with the IP range
        checkPublicIpRangeErrors(zoneId, vlanId, vlanGateway, vlanNetmask, startIP, endIP);
        // Throw an exception if this subnet overlaps with subnet on other VLAN,
        // if this is ip range extension, gateway, network mask should be same and ip range should not overlap
        final List<VlanVO> vlans = _vlanDao.listByZone(zone.getId());
        for (final VlanVO vlan : vlans) {
            final String otherVlanGateway = vlan.getVlanGateway();
            final String otherVlanNetmask = vlan.getVlanNetmask();
            // Continue if it's not IPv4
            if (otherVlanGateway == null || otherVlanNetmask == null) {
                continue;
            }
            if (vlan.getNetworkId() == null) {
                continue;
            }
            final String otherCidr = NetUtils.getCidrFromGatewayAndNetmask(otherVlanGateway, otherVlanNetmask);
            if (!NetUtils.isNetworksOverlap(newCidr, otherCidr)) {
                continue;
            }
            // from here, subnet overlaps
            if (!vlanId.equals(vlan.getVlanTag())) {
                boolean overlapped = false;
                if (network.getTrafficType() == TrafficType.Public) {
                    overlapped = true;
                } else {
                    final Long nwId = vlan.getNetworkId();
                    if (nwId != null) {
                        final Network nw = _networkModel.getNetwork(nwId);
                        if (nw != null && nw.getTrafficType() == TrafficType.Public) {
                            overlapped = true;
                        }
                    }
                }
                if (overlapped) {
                    throw new InvalidParameterValueException("The IP range with tag: " + vlan.getVlanTag() + " in zone " + zone.getName() + " has overlapped with the subnet. Please specify a different gateway/netmask.");
                }
            } else {
                final String[] otherVlanIpRange = vlan.getIpRange().split("\\-");
                final String otherVlanStartIP = otherVlanIpRange[0];
                String otherVlanEndIP = null;
                if (otherVlanIpRange.length > 1) {
                    otherVlanEndIP = otherVlanIpRange[1];
                }
                // extend IP range
                if (!vlanGateway.equals(otherVlanGateway) || !vlanNetmask.equals(vlan.getVlanNetmask())) {
                    throw new InvalidParameterValueException("The IP range has already been added with gateway " + otherVlanGateway + " ,and netmask " + otherVlanNetmask + ", Please specify the gateway/netmask if you want to extend ip range");
                }
                if (!NetUtils.is31PrefixCidr(newCidr)) {
                    if (NetUtils.ipRangesOverlap(startIP, endIP, otherVlanStartIP, otherVlanEndIP)) {
                        throw new InvalidParameterValueException("The IP range already has IPs that overlap with the new range." + " Please specify a different start IP/end IP.");
                    }
                }
            }
        }
    }
    String ipv6Range = null;
    if (ipv6) {
        ipv6Range = startIPv6;
        if (endIPv6 != null) {
            ipv6Range += "-" + endIPv6;
        }
        final List<VlanVO> vlans = _vlanDao.listByZone(zone.getId());
        for (final VlanVO vlan : vlans) {
            if (vlan.getIp6Gateway() == null) {
                continue;
            }
            if (NetUtils.isSameIsolationId(vlanId, vlan.getVlanTag())) {
                if (NetUtils.isIp6RangeOverlap(ipv6Range, vlan.getIp6Range())) {
                    throw new InvalidParameterValueException("The IPv6 range with tag: " + vlan.getVlanTag() + " already has IPs that overlap with the new range. Please specify a different start IP/end IP.");
                }
                if (!vlanIp6Gateway.equals(vlan.getIp6Gateway())) {
                    throw new InvalidParameterValueException("The IP range with tag: " + vlan.getVlanTag() + " has already been added with gateway " + vlan.getIp6Gateway() + ". Please specify a different tag.");
                }
            }
        }
    }
    // Check if the vlan is being used
    if (_zoneDao.findVnet(zoneId, physicalNetworkId, vlanId).size() > 0) {
        throw new InvalidParameterValueException("The VLAN tag " + vlanId + " is already being used for dynamic vlan allocation for the guest network in zone " + zone.getName());
    }
    String ipRange = null;
    if (ipv4) {
        ipRange = startIP;
        if (endIP != null) {
            ipRange += "-" + endIP;
        }
    }
    // Everything was fine, so persist the VLAN
    final VlanVO vlan = commitVlanAndIpRange(zoneId, networkId, physicalNetworkId, podId, startIP, endIP, vlanGateway, vlanNetmask, vlanId, domain, vlanOwner, vlanIp6Gateway, vlanIp6Cidr, ipv4, zone, vlanType, ipv6Range, ipRange);
    return vlan;
}
Also used : Pod(com.cloud.legacymodel.dc.Pod) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) Zone(com.cloud.db.model.Zone) PhysicalNetwork(com.cloud.network.PhysicalNetwork) Network(com.cloud.legacymodel.network.Network) VlanVO(com.cloud.dc.VlanVO) URI(java.net.URI) VlanType(com.cloud.legacymodel.dc.Vlan.VlanType) DB(com.cloud.utils.db.DB)

Example 2 with VlanType

use of com.cloud.legacymodel.dc.Vlan.VlanType in project cosmic by MissionCriticalCloud.

the class IpAddressManagerImpl method allocateIp.

@DB
@Override
public IpAddress allocateIp(final Account ipOwner, final boolean isSystem, final Account caller, final long callerUserId, final DataCenter zone, final Boolean displayIp) throws ConcurrentOperationException, ResourceAllocationException, InsufficientAddressCapacityException {
    final VlanType vlanType = VlanType.VirtualNetwork;
    final boolean assign = false;
    if (AllocationState.Disabled == zone.getAllocationState() && !_accountMgr.isRootAdmin(caller.getId())) {
        // zone is of type DataCenter. See DataCenterVO.java.
        final PermissionDeniedException ex = new PermissionDeniedException("Cannot perform this operation, " + "Zone is currently disabled");
        ex.addProxyObject(zone.getUuid(), "zoneId");
        throw ex;
    }
    PublicIp ip = null;
    Account accountToLock = null;
    try {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Associate IP address called by the user " + callerUserId + " account " + ipOwner.getId());
        }
        accountToLock = _accountDao.acquireInLockTable(ipOwner.getId());
        if (accountToLock == null) {
            s_logger.warn("Unable to lock account: " + ipOwner.getId());
            throw new ConcurrentOperationException("Unable to acquire account lock");
        }
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Associate IP address lock acquired");
        }
        ip = Transaction.execute(new TransactionCallbackWithException<PublicIp, InsufficientAddressCapacityException>() {

            @Override
            public PublicIp doInTransaction(final TransactionStatus status) throws InsufficientAddressCapacityException {
                final PublicIp ip = fetchNewPublicIp(zone.getId(), null, null, ipOwner, vlanType, null, false, assign, null, isSystem, null, displayIp);
                if (ip == null) {
                    final InsufficientAddressCapacityException ex = new InsufficientAddressCapacityException("Unable to find available public IP addresses", DataCenter.class, zone.getId());
                    ex.addProxyObject(ApiDBUtils.findZoneById(zone.getId()).getUuid());
                    throw ex;
                }
                CallContext.current().setEventDetails("Ip Id: " + ip.getId());
                final Ip ipAddress = ip.getAddress();
                s_logger.debug("Got " + ipAddress + " to assign for account " + ipOwner.getId() + " in zone " + zone.getId());
                return ip;
            }
        });
    } finally {
        if (accountToLock != null) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Releasing lock account " + ipOwner);
            }
            _accountDao.releaseFromLockTable(ipOwner.getId());
            s_logger.debug("Associate IP address lock released");
        }
    }
    return ip;
}
Also used : Account(com.cloud.legacymodel.user.Account) PublicIp(com.cloud.network.addr.PublicIp) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) InsufficientAddressCapacityException(com.cloud.legacymodel.exceptions.InsufficientAddressCapacityException) Ip(com.cloud.legacymodel.network.Ip) PublicIp(com.cloud.network.addr.PublicIp) TransactionStatus(com.cloud.utils.db.TransactionStatus) PermissionDeniedException(com.cloud.legacymodel.exceptions.PermissionDeniedException) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) VlanType(com.cloud.legacymodel.dc.Vlan.VlanType) DB(com.cloud.utils.db.DB)

Example 3 with VlanType

use of com.cloud.legacymodel.dc.Vlan.VlanType in project cosmic by MissionCriticalCloud.

the class ManagementServerImpl method searchForIPAddresses.

@Override
public Pair<List<? extends IpAddress>, Integer> searchForIPAddresses(final ListPublicIpAddressesCmd cmd) {
    final Object keyword = cmd.getKeyword();
    final Long physicalNetworkId = cmd.getPhysicalNetworkId();
    final Long associatedNetworkId = cmd.getAssociatedNetworkId();
    final Long zone = cmd.getZoneId();
    final String address = cmd.getIpAddress();
    final Long vlan = cmd.getVlanId();
    final Boolean forVirtualNetwork = cmd.isForVirtualNetwork();
    final Boolean forLoadBalancing = cmd.isForLoadBalancing();
    final Long ipId = cmd.getId();
    final Boolean sourceNat = cmd.getIsSourceNat();
    final Boolean staticNat = cmd.getIsStaticNat();
    final Long vpcId = cmd.getVpcId();
    final Boolean forDisplay = cmd.getDisplay();
    final Map<String, String> tags = cmd.getTags();
    final String state = cmd.getState();
    Boolean isAllocated = cmd.isAllocatedOnly();
    if (isAllocated == null) {
        isAllocated = Boolean.TRUE;
        if (state != null) {
            isAllocated = Boolean.FALSE;
        }
    }
    final Filter searchFilter = new Filter(IPAddressVO.class, "address", false, cmd.getStartIndex(), cmd.getPageSizeVal());
    final SearchBuilder<IPAddressVO> sb = _publicIpAddressDao.createSearchBuilder();
    Long domainId = null;
    Boolean isRecursive = null;
    final List<Long> permittedAccounts = new ArrayList<>();
    ListProjectResourcesCriteria listProjectResourcesCriteria = null;
    if (isAllocated) {
        final Account caller = getCaller();
        final Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<>(cmd.getDomainId(), cmd.isRecursive(), null);
        _accountMgr.buildACLSearchParameters(caller, cmd.getId(), cmd.getAccountName(), cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, cmd.listAll(), false);
        domainId = domainIdRecursiveListProject.first();
        isRecursive = domainIdRecursiveListProject.second();
        listProjectResourcesCriteria = domainIdRecursiveListProject.third();
        _accountMgr.buildACLSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    }
    sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ);
    sb.and("address", sb.entity().getAddress(), SearchCriteria.Op.EQ);
    sb.and("vlanDbId", sb.entity().getVlanId(), SearchCriteria.Op.EQ);
    sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
    sb.and("physicalNetworkId", sb.entity().getPhysicalNetworkId(), SearchCriteria.Op.EQ);
    sb.and("associatedNetworkIdEq", sb.entity().getAssociatedWithNetworkId(), SearchCriteria.Op.EQ);
    sb.and("isSourceNat", sb.entity().isSourceNat(), SearchCriteria.Op.EQ);
    sb.and("isStaticNat", sb.entity().isOneToOneNat(), SearchCriteria.Op.EQ);
    sb.and("vpcId", sb.entity().getVpcId(), SearchCriteria.Op.EQ);
    sb.and("state", sb.entity().getState(), SearchCriteria.Op.EQ);
    sb.and("display", sb.entity().isDisplay(), SearchCriteria.Op.EQ);
    if (forLoadBalancing != null && forLoadBalancing) {
        final SearchBuilder<LoadBalancerVO> lbSearch = _loadbalancerDao.createSearchBuilder();
        sb.join("lbSearch", lbSearch, sb.entity().getId(), lbSearch.entity().getSourceIpAddressId(), JoinType.INNER);
        sb.groupBy(sb.entity().getId());
    }
    if (keyword != null && address == null) {
        sb.and("addressLIKE", sb.entity().getAddress(), SearchCriteria.Op.LIKE);
    }
    if (tags != null && !tags.isEmpty()) {
        final SearchBuilder<ResourceTagVO> tagSearch = _resourceTagDao.createSearchBuilder();
        for (int count = 0; count < tags.size(); count++) {
            tagSearch.or().op("key" + String.valueOf(count), tagSearch.entity().getKey(), SearchCriteria.Op.EQ);
            tagSearch.and("value" + String.valueOf(count), tagSearch.entity().getValue(), SearchCriteria.Op.EQ);
            tagSearch.cp();
        }
        tagSearch.and("resourceType", tagSearch.entity().getResourceType(), SearchCriteria.Op.EQ);
        sb.groupBy(sb.entity().getId());
        sb.join("tagSearch", tagSearch, sb.entity().getId(), tagSearch.entity().getResourceId(), JoinBuilder.JoinType.INNER);
    }
    final SearchBuilder<VlanVO> vlanSearch = _vlanDao.createSearchBuilder();
    vlanSearch.and("vlanType", vlanSearch.entity().getVlanType(), SearchCriteria.Op.EQ);
    sb.join("vlanSearch", vlanSearch, sb.entity().getVlanId(), vlanSearch.entity().getId(), JoinBuilder.JoinType.INNER);
    if (isAllocated != null && isAllocated == true) {
        sb.and("allocated", sb.entity().getAllocatedTime(), SearchCriteria.Op.NNULL);
    }
    final VlanType vlanType;
    if (forVirtualNetwork != null) {
        vlanType = forVirtualNetwork ? VlanType.VirtualNetwork : VlanType.DirectAttached;
    } else {
        vlanType = VlanType.VirtualNetwork;
    }
    final SearchCriteria<IPAddressVO> sc = sb.create();
    if (isAllocated) {
        _accountMgr.buildACLSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    }
    sc.setJoinParameters("vlanSearch", "vlanType", vlanType);
    if (tags != null && !tags.isEmpty()) {
        int count = 0;
        sc.setJoinParameters("tagSearch", "resourceType", ResourceObjectType.PublicIpAddress.toString());
        for (final String key : tags.keySet()) {
            sc.setJoinParameters("tagSearch", "key" + String.valueOf(count), key);
            sc.setJoinParameters("tagSearch", "value" + String.valueOf(count), tags.get(key));
            count++;
        }
    }
    if (zone != null) {
        sc.setParameters("dataCenterId", zone);
    }
    if (vpcId != null) {
        sc.setParameters("vpcId", vpcId);
    }
    if (ipId != null) {
        sc.setParameters("id", ipId);
    }
    if (sourceNat != null) {
        sc.setParameters("isSourceNat", sourceNat);
    }
    if (staticNat != null) {
        sc.setParameters("isStaticNat", staticNat);
    }
    if (address == null && keyword != null) {
        sc.setParameters("addressLIKE", "%" + keyword + "%");
    }
    if (address != null) {
        sc.setParameters("address", address);
    }
    if (vlan != null) {
        sc.setParameters("vlanDbId", vlan);
    }
    if (physicalNetworkId != null) {
        sc.setParameters("physicalNetworkId", physicalNetworkId);
    }
    if (associatedNetworkId != null) {
        sc.setParameters("associatedNetworkIdEq", associatedNetworkId);
    }
    if (forDisplay != null) {
        sc.setParameters("display", forDisplay);
    }
    if (state != null) {
        sc.setParameters("state", state);
    }
    final Pair<List<IPAddressVO>, Integer> result = _publicIpAddressDao.searchAndCount(sc, searchFilter);
    return new Pair<>(result.first(), result.second());
}
Also used : Account(com.cloud.legacymodel.user.Account) ArrayList(java.util.ArrayList) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) ResourceTagVO(com.cloud.tags.ResourceTagVO) ArrayList(java.util.ArrayList) ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) List(java.util.List) VlanVO(com.cloud.dc.VlanVO) VlanType(com.cloud.legacymodel.dc.Vlan.VlanType) SSHKeyPair(com.cloud.legacymodel.user.SSHKeyPair) Pair(com.cloud.legacymodel.utils.Pair) Ternary(com.cloud.legacymodel.utils.Ternary) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) Filter(com.cloud.utils.db.Filter) IPAddressVO(com.cloud.network.dao.IPAddressVO)

Aggregations

VlanType (com.cloud.legacymodel.dc.Vlan.VlanType)3 VlanVO (com.cloud.dc.VlanVO)2 Account (com.cloud.legacymodel.user.Account)2 DB (com.cloud.utils.db.DB)2 Zone (com.cloud.db.model.Zone)1 ExcludeList (com.cloud.deploy.DeploymentPlanner.ExcludeList)1 Pod (com.cloud.legacymodel.dc.Pod)1 ConcurrentOperationException (com.cloud.legacymodel.exceptions.ConcurrentOperationException)1 InsufficientAddressCapacityException (com.cloud.legacymodel.exceptions.InsufficientAddressCapacityException)1 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)1 PermissionDeniedException (com.cloud.legacymodel.exceptions.PermissionDeniedException)1 Ip (com.cloud.legacymodel.network.Ip)1 Network (com.cloud.legacymodel.network.Network)1 SSHKeyPair (com.cloud.legacymodel.user.SSHKeyPair)1 Pair (com.cloud.legacymodel.utils.Pair)1 Ternary (com.cloud.legacymodel.utils.Ternary)1 PhysicalNetwork (com.cloud.network.PhysicalNetwork)1 PublicIp (com.cloud.network.addr.PublicIp)1 IPAddressVO (com.cloud.network.dao.IPAddressVO)1 LoadBalancerVO (com.cloud.network.dao.LoadBalancerVO)1