Search in sources :

Example 46 with VlanVO

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

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<Long>();
    ListProjectResourcesCriteria listProjectResourcesCriteria = null;
    if (isAllocated) {
        final Account caller = getCaller();
        final Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(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);
    boolean allocatedOnly = false;
    if (isAllocated != null && isAllocated == true) {
        sb.and("allocated", sb.entity().getAllocatedTime(), SearchCriteria.Op.NNULL);
        allocatedOnly = true;
    }
    VlanType vlanType = null;
    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<List<? extends IpAddress>, Integer>(result.first(), result.second());
}
Also used : Account(com.cloud.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.dc.Vlan.VlanType) Pair(com.cloud.utils.Pair) SSHKeyPair(com.cloud.user.SSHKeyPair) Ternary(com.cloud.utils.Ternary) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) Filter(com.cloud.utils.db.Filter) IPAddressVO(com.cloud.network.dao.IPAddressVO)

Example 47 with VlanVO

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

the class ResourceLimitManagerImpl method calculatePublicIpForAccount.

private long calculatePublicIpForAccount(long accountId) {
    Long dedicatedCount = 0L;
    Long allocatedCount = 0L;
    List<VlanVO> dedicatedVlans = _vlanDao.listDedicatedVlans(accountId);
    for (VlanVO dedicatedVlan : dedicatedVlans) {
        List<IPAddressVO> ips = _ipAddressDao.listByVlanId(dedicatedVlan.getId());
        dedicatedCount += new Long(ips.size());
    }
    allocatedCount = _ipAddressDao.countAllocatedIPsForAccount(accountId);
    if (dedicatedCount > allocatedCount)
        return dedicatedCount;
    else
        return allocatedCount;
}
Also used : IPAddressVO(com.cloud.network.dao.IPAddressVO) VlanVO(com.cloud.dc.VlanVO)

Example 48 with VlanVO

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

the class CiscoVnmcElementTest method applyStaticNatsTest.

@Test
public void applyStaticNatsTest() throws ResourceUnavailableException {
    URI uri = URI.create("vlan://123");
    Network network = mock(Network.class);
    when(network.getId()).thenReturn(1L);
    when(network.getBroadcastDomainType()).thenReturn(BroadcastDomainType.Vlan);
    when(network.getDataCenterId()).thenReturn(1L);
    when(network.getBroadcastUri()).thenReturn(uri);
    when(network.getCidr()).thenReturn("1.1.1.0/24");
    when(network.getState()).thenReturn(Network.State.Implemented);
    Ip ip = mock(Ip.class);
    when(ip.addr()).thenReturn("1.2.3.4");
    IpAddress ipAddress = mock(IpAddress.class);
    when(ipAddress.getAddress()).thenReturn(ip);
    when(ipAddress.getVlanId()).thenReturn(1L);
    when(_networkModel.getIp(anyLong())).thenReturn(ipAddress);
    when(_networkModel.isProviderSupportServiceInNetwork(network.getId(), Service.StaticNat, Provider.CiscoVnmc)).thenReturn(true);
    List<CiscoVnmcControllerVO> devices = new ArrayList<CiscoVnmcControllerVO>();
    devices.add(mock(CiscoVnmcControllerVO.class));
    when(_ciscoVnmcDao.listByPhysicalNetwork(network.getPhysicalNetworkId())).thenReturn(devices);
    when(_networkAsa1000vMapDao.findByNetworkId(network.getId())).thenReturn(mock(NetworkAsa1000vMapVO.class));
    HostVO hostVO = mock(HostVO.class);
    when(hostVO.getId()).thenReturn(1L);
    when(_hostDao.findById(anyLong())).thenReturn(hostVO);
    VlanVO vlanVO = mock(VlanVO.class);
    when(vlanVO.getVlanTag()).thenReturn(null);
    when(_vlanDao.findById(anyLong())).thenReturn(vlanVO);
    StaticNat rule = mock(StaticNat.class);
    when(rule.getSourceIpAddressId()).thenReturn(1L);
    when(rule.getDestIpAddress()).thenReturn("1.2.3.4");
    when(rule.isForRevoke()).thenReturn(false);
    List<StaticNat> rules = new ArrayList<StaticNat>();
    rules.add(rule);
    Answer answer = mock(Answer.class);
    when(answer.getResult()).thenReturn(true);
    when(_agentMgr.easySend(anyLong(), any(SetStaticNatRulesCommand.class))).thenReturn(answer);
    assertTrue(_element.applyStaticNats(network, rules));
}
Also used : SetStaticNatRulesCommand(com.cloud.agent.api.routing.SetStaticNatRulesCommand) Ip(com.cloud.utils.net.Ip) PublicIp(com.cloud.network.addr.PublicIp) ArrayList(java.util.ArrayList) URI(java.net.URI) HostVO(com.cloud.host.HostVO) StaticNat(com.cloud.network.rules.StaticNat) Answer(com.cloud.agent.api.Answer) Network(com.cloud.network.Network) NetworkAsa1000vMapVO(com.cloud.network.cisco.NetworkAsa1000vMapVO) IpAddress(com.cloud.network.IpAddress) CiscoVnmcControllerVO(com.cloud.network.cisco.CiscoVnmcControllerVO) VlanVO(com.cloud.dc.VlanVO) Test(org.junit.Test)

Example 49 with VlanVO

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

the class NiciraNvpElement method sharedNetworkSupportNumericalVlanId.

private boolean sharedNetworkSupportNumericalVlanId(Network network, String lSwitchUuid, String ownerName, HostVO niciraNvpHost) {
    List<VlanVO> networkVlans = vlanDao.listVlansByNetworkId(network.getId());
    if (networkVlans.size() == 1) {
        for (VlanVO vlanVO : networkVlans) {
            long vlanId = Long.parseLong(vlanVO.getVlanTag());
            String l2GatewayServiceUuid = niciraNvpHost.getDetail("l2gatewayserviceuuid");
            if (l2GatewayServiceUuid == null) {
                throw new CloudRuntimeException("No L2 Gateway Service Uuid found on " + niciraNvpHost.getName());
            }
            ConfigureSharedNetworkVlanIdCommand cmd = new ConfigureSharedNetworkVlanIdCommand(lSwitchUuid, l2GatewayServiceUuid, vlanId, ownerName, network.getId());
            ConfigureSharedNetworkVlanIdAnswer answer = (ConfigureSharedNetworkVlanIdAnswer) agentMgr.easySend(niciraNvpHost.getId(), cmd);
            if (answer.getResult() == false) {
                s_logger.error("Failed to configure Shared network " + network.getDisplayText());
                return false;
            }
        }
    }
    return true;
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ConfigureSharedNetworkVlanIdCommand(com.cloud.agent.api.ConfigureSharedNetworkVlanIdCommand) VlanVO(com.cloud.dc.VlanVO) ConfigureSharedNetworkVlanIdAnswer(com.cloud.agent.api.ConfigureSharedNetworkVlanIdAnswer)

Example 50 with VlanVO

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

the class ApiResponseHelper method createIPAddressResponse.

@Override
public IPAddressResponse createIPAddressResponse(ResponseView view, IpAddress ipAddr) {
    VlanVO vlan = ApiDBUtils.findVlanById(ipAddr.getVlanId());
    boolean forVirtualNetworks = vlan.getVlanType().equals(VlanType.VirtualNetwork);
    long zoneId = ipAddr.getDataCenterId();
    IPAddressResponse ipResponse = new IPAddressResponse();
    ipResponse.setId(ipAddr.getUuid());
    ipResponse.setIpAddress(ipAddr.getAddress().toString());
    if (ipAddr.getAllocatedTime() != null) {
        ipResponse.setAllocated(ipAddr.getAllocatedTime());
    }
    DataCenter zone = ApiDBUtils.findZoneById(ipAddr.getDataCenterId());
    if (zone != null) {
        ipResponse.setZoneId(zone.getUuid());
        ipResponse.setZoneName(zone.getName());
    }
    ipResponse.setSourceNat(ipAddr.isSourceNat());
    ipResponse.setIsSystem(ipAddr.getSystem());
    // get account information
    if (ipAddr.getAllocatedToAccountId() != null) {
        populateOwner(ipResponse, ipAddr);
    }
    ipResponse.setForVirtualNetwork(forVirtualNetworks);
    ipResponse.setStaticNat(ipAddr.isOneToOneNat());
    if (ipAddr.getAssociatedWithVmId() != null) {
        UserVm vm = ApiDBUtils.findUserVmById(ipAddr.getAssociatedWithVmId());
        if (vm != null) {
            ipResponse.setVirtualMachineId(vm.getUuid());
            ipResponse.setVirtualMachineName(vm.getHostName());
            if (vm.getDisplayName() != null) {
                ipResponse.setVirtualMachineDisplayName(vm.getDisplayName());
            } else {
                ipResponse.setVirtualMachineDisplayName(vm.getHostName());
            }
        }
    }
    if (ipAddr.getVmIp() != null) {
        ipResponse.setVirtualMachineIp(ipAddr.getVmIp());
    }
    if (ipAddr.getAssociatedWithNetworkId() != null) {
        Network ntwk = ApiDBUtils.findNetworkById(ipAddr.getAssociatedWithNetworkId());
        if (ntwk != null) {
            ipResponse.setAssociatedNetworkId(ntwk.getUuid());
            ipResponse.setAssociatedNetworkName(ntwk.getName());
        }
    }
    if (ipAddr.getVpcId() != null) {
        Vpc vpc = ApiDBUtils.findVpcById(ipAddr.getVpcId());
        if (vpc != null) {
            ipResponse.setVpcId(vpc.getUuid());
        }
    }
    // Network id the ip is associated with (if associated networkId is
    // null, try to get this information from vlan)
    Long vlanNetworkId = ApiDBUtils.getVlanNetworkId(ipAddr.getVlanId());
    // Network id the ip belongs to
    Long networkId;
    if (vlanNetworkId != null) {
        networkId = vlanNetworkId;
    } else {
        networkId = ApiDBUtils.getPublicNetworkIdByZone(zoneId);
    }
    if (networkId != null) {
        NetworkVO nw = ApiDBUtils.findNetworkById(networkId);
        if (nw != null) {
            ipResponse.setNetworkId(nw.getUuid());
        }
    }
    ipResponse.setState(ipAddr.getState().toString());
    if (ipAddr.getPhysicalNetworkId() != null) {
        PhysicalNetworkVO pnw = ApiDBUtils.findPhysicalNetworkById(ipAddr.getPhysicalNetworkId());
        if (pnw != null) {
            ipResponse.setPhysicalNetworkId(pnw.getUuid());
        }
    }
    // show this info to full view only
    if (view == ResponseView.Full) {
        VlanVO vl = ApiDBUtils.findVlanById(ipAddr.getVlanId());
        if (vl != null) {
            ipResponse.setVlanId(vl.getUuid());
            ipResponse.setVlanName(vl.getVlanTag());
        }
    }
    if (ipAddr.getSystem()) {
        if (ipAddr.isOneToOneNat()) {
            ipResponse.setPurpose(IpAddress.Purpose.StaticNat.toString());
        } else {
            ipResponse.setPurpose(IpAddress.Purpose.Lb.toString());
        }
    }
    ipResponse.setForDisplay(ipAddr.isDisplay());
    ipResponse.setPortable(ipAddr.isPortable());
    //set tag information
    List<? extends ResourceTag> tags = ApiDBUtils.listByResourceTypeAndId(ResourceObjectType.PublicIpAddress, ipAddr.getId());
    List<ResourceTagResponse> tagResponses = new ArrayList<ResourceTagResponse>();
    for (ResourceTag tag : tags) {
        ResourceTagResponse tagResponse = createResourceTagResponse(tag, true);
        CollectionUtils.addIgnoreNull(tagResponses, tagResponse);
    }
    ipResponse.setTags(tagResponses);
    ipResponse.setObjectName("ipaddress");
    return ipResponse;
}
Also used : PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) Vpc(com.cloud.network.vpc.Vpc) ArrayList(java.util.ArrayList) UserVm(com.cloud.uservm.UserVm) DataCenter(com.cloud.dc.DataCenter) ResourceTag(com.cloud.server.ResourceTag) Network(com.cloud.network.Network) PhysicalNetwork(com.cloud.network.PhysicalNetwork) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) ResourceTagResponse(org.apache.cloudstack.api.response.ResourceTagResponse) VlanVO(com.cloud.dc.VlanVO) IPAddressResponse(org.apache.cloudstack.api.response.IPAddressResponse)

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